Skip to content

Commit

Permalink
- 2f6a3c92ce0f64e0923ad326ec5d07eae03061fd Add C++14's std::exchange…
Browse files Browse the repository at this point in the history
… to absl/utility/utility.h. by Abseil Team <absl-team@google.com>

  - 9f2c9adbf998dd9062c8cc3da1076bde87081d71 Fix comment typo. by Greg Falcon <gfalcon@google.com>

GitOrigin-RevId: 2f6a3c92ce0f64e0923ad326ec5d07eae03061fd
Change-Id: I7b9db94ad315092baefe56b02f250acadafbe312
  • Loading branch information
Abseil Team authored and derekmauro committed Apr 25, 2018
1 parent 19b3c95 commit ea0e750
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion absl/debugging/symbolize_win32.inc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void InitializeSymbolizer(const char *argv0) {
SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_UNDNAME);
if (!SymInitialize(process, nullptr, true)) {
// GetLastError() returns a Win32 DWORD, but we assign to
// unsigned long to simplify the ABSL_RAW_LOG case below. The uniform
// unsigned long long to simplify the ABSL_RAW_LOG case below. The uniform
// initialization guarantees this is not a narrowing conversion.
const unsigned long long error{GetLastError()}; // NOLINT(runtime/int)
ABSL_RAW_LOG(FATAL, "SymInitialize() failed: %llu", error);
Expand Down
22 changes: 22 additions & 0 deletions absl/utility/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// * make_index_sequence<N> == std::make_index_sequence<N>
// * index_sequence_for<Ts...> == std::index_sequence_for<Ts...>
// * apply<Functor, Tuple> == std::apply<Functor, Tuple>
// * exchange<T> == std::exchange<T>
//
// This header file also provides the tag types `in_place_t`, `in_place_type_t`,
// and `in_place_index_t`, as well as the constant `in_place`, and
Expand Down Expand Up @@ -264,6 +265,27 @@ auto apply(Functor&& functor, Tuple&& t)
absl::make_index_sequence<std::tuple_size<
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 absl

#endif // ABSL_UTILITY_UTILITY_H_
8 changes: 8 additions & 0 deletions absl/utility/utility_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,5 +333,13 @@ TEST(ApplyTest, FlipFlop) {
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);
}

} // namespace

0 comments on commit ea0e750

Please sign in to comment.