Commit 377de9d7 by Derek Mauro Committed by Copybara-Service

Deprecate `absl::exchange`, `absl::forward` and `absl::move`, which

were only useful before C++14.

Callers should use `std::exchange`, `std::forward` and `std::move`
instead.

One thing to note is that some compilers issue warnings about pessimizing
and redundant moves. Some compilers were able to apply this analysis to
std::move but not absl::move. If you get a warning about one of these issues
now that absl::move is an alias for std::move, you should remove this use of
move.
See https://developers.redhat.com/blog/2019/04/12/understanding-when-not-to-stdmove-in-c
PiperOrigin-RevId: 621861324
Change-Id: I60f98b59be5ff425bd17fbce43d9218c361720c2
parent d5e42609
...@@ -51,11 +51,14 @@ ABSL_NAMESPACE_BEGIN ...@@ -51,11 +51,14 @@ ABSL_NAMESPACE_BEGIN
// abstractions for platforms that had not yet provided them. Those // abstractions for platforms that had not yet provided them. Those
// platforms are no longer supported. New code should simply use the // platforms are no longer supported. New code should simply use the
// the ones from std directly. // the ones from std directly.
using std::exchange;
using std::forward;
using std::index_sequence; using std::index_sequence;
using std::index_sequence_for; using std::index_sequence_for;
using std::integer_sequence; using std::integer_sequence;
using std::make_index_sequence; using std::make_index_sequence;
using std::make_integer_sequence; using std::make_integer_sequence;
using std::move;
namespace utility_internal { namespace utility_internal {
...@@ -129,27 +132,6 @@ template <size_t I> ...@@ -129,27 +132,6 @@ template <size_t I>
void in_place_index(utility_internal::InPlaceIndexTag<I>) {} void in_place_index(utility_internal::InPlaceIndexTag<I>) {}
#endif // ABSL_USES_STD_VARIANT #endif // ABSL_USES_STD_VARIANT
// Constexpr move and forward
// move()
//
// A constexpr version of `std::move()`, designed to be a drop-in replacement
// for C++14's `std::move()`.
template <typename T>
constexpr absl::remove_reference_t<T>&& move(T&& t) noexcept {
return static_cast<absl::remove_reference_t<T>&&>(t);
}
// forward()
//
// A constexpr version of `std::forward()`, designed to be a drop-in replacement
// for C++14's `std::forward()`.
template <typename T>
constexpr T&& forward(
absl::remove_reference_t<T>& t) noexcept { // NOLINT(runtime/references)
return static_cast<T&&>(t);
}
namespace utility_internal { namespace utility_internal {
// Helper method for expanding tuple into a called method. // Helper method for expanding tuple into a called method.
template <typename Functor, typename Tuple, std::size_t... Indexes> template <typename Functor, typename Tuple, std::size_t... Indexes>
...@@ -215,26 +197,6 @@ auto apply(Functor&& functor, Tuple&& t) ...@@ -215,26 +197,6 @@ auto apply(Functor&& functor, Tuple&& t)
typename std::remove_reference<Tuple>::type>::value>{}); typename std::remove_reference<Tuple>::type>::value>{});
} }
// exchange
//
// Replaces the value of `obj` with `new_value` and returns the old value of
// `obj`. `absl::exchange` is designed to be a drop-in replacement for C++14's
// `std::exchange`.
//
// Example:
//
// Foo& operator=(Foo&& other) {
// ptr1_ = absl::exchange(other.ptr1_, nullptr);
// int1_ = absl::exchange(other.int1_, -1);
// return *this;
// }
template <typename T, typename U = T>
T exchange(T& obj, U&& new_value) {
T old_value = absl::move(obj);
obj = absl::forward<U>(new_value);
return old_value;
}
namespace utility_internal { namespace utility_internal {
template <typename T, typename Tuple, size_t... I> template <typename T, typename Tuple, size_t... I>
T make_from_tuple_impl(Tuple&& tup, absl::index_sequence<I...>) { T make_from_tuple_impl(Tuple&& tup, absl::index_sequence<I...>) {
......
...@@ -205,14 +205,6 @@ TEST(ApplyTest, FlipFlop) { ...@@ -205,14 +205,6 @@ TEST(ApplyTest, FlipFlop) {
EXPECT_EQ(42, absl::apply(&FlipFlop::member, std::make_tuple(obj))); EXPECT_EQ(42, absl::apply(&FlipFlop::member, std::make_tuple(obj)));
} }
TEST(ExchangeTest, MoveOnly) {
auto a = Factory(1);
EXPECT_EQ(1, *a);
auto b = absl::exchange(a, Factory(2));
EXPECT_EQ(2, *a);
EXPECT_EQ(1, *b);
}
TEST(MakeFromTupleTest, String) { TEST(MakeFromTupleTest, String) {
EXPECT_EQ( EXPECT_EQ(
absl::make_from_tuple<std::string>(std::make_tuple("hello world", 5)), absl::make_from_tuple<std::string>(std::make_tuple("hello world", 5)),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment