Commit 16e98a0d authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Backport std::exchange

Summary: [Folly] Backport `std::exchange` to `folly/Utility.h`.

Reviewed By: aary

Differential Revision: D7722731

fbshipit-source-id: a4a68bfb9ec8b356b77f4a73bdadb7f2807d517f
parent 4037ae31
......@@ -101,6 +101,23 @@ void as_const(T const&&) = delete;
#endif
#if __cpp_lib_exchange_function || _LIBCPP_STD_VER > 11 || _MSC_VER
/* using override */ using std::exchange;
#else
// mimic: std::exchange, C++14
// from: http://en.cppreference.com/w/cpp/utility/exchange, CC-BY-SA
template <class T, class U = T>
T exchange(T& obj, U&& new_value) {
T old_value = std::move(obj);
obj = std::forward<U>(new_value);
return old_value;
}
#endif
namespace utility_detail {
template <typename...>
struct make_seq_cat;
......
......@@ -77,6 +77,13 @@ TEST_F(UtilityTest, as_const) {
EXPECT_TRUE(noexcept(folly::as_const(s)));
}
TEST_F(UtilityTest, exchange) {
auto obj = std::map<std::string, int>{{"hello", 3}};
auto old = exchange(obj, {{"world", 4}});
EXPECT_EQ((std::map<std::string, int>{{"world", 4}}), obj);
EXPECT_EQ((std::map<std::string, int>{{"hello", 3}}), old);
}
TEST(FollyIntegerSequence, core) {
constexpr auto seq = folly::integer_sequence<int, 0, 3, 2>();
static_assert(seq.size() == 3, "");
......
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