1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___UTILITY_EXCHANGE_H
10#define _LIBCPP___UTILITY_EXCHANGE_H
11
12#include <__config>
13#include <__type_traits/enable_if.h>
14#include <__type_traits/is_assignable.h>
15#include <__type_traits/is_constructible.h>
16#include <__type_traits/is_nothrow_assignable.h>
17#include <__type_traits/is_nothrow_constructible.h>
18#include <__utility/forward.h>
19#include <__utility/move.h>
20
21#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22# pragma GCC system_header
23#endif
24
25_LIBCPP_PUSH_MACROS
26#include <__undef_macros>
27
28_LIBCPP_BEGIN_NAMESPACE_STD
29
30template <class _T1, class _T2 = _T1>
31[[__nodiscard__]] _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _T1
32__exchange(_T1& __obj, _T2&& __new_value)
33 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value&& is_nothrow_assignable<_T1&, _T2>::value) {
34 _T1 __old_value = std::move(__obj);
35 __obj = std::forward<_T2>(__new_value);
36 return __old_value;
37}
38
39#if _LIBCPP_STD_VER >= 14
40template <class _T1, class _T2 = _T1>
41[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
42_LIBCPP_CONSTEXPR_SINCE_CXX20 _T1 exchange(_T1& __obj, _T2&& __new_value) noexcept(
43 is_nothrow_move_constructible<_T1>::value && is_nothrow_assignable<_T1&, _T2>::value) {
44 return std::__exchange(__obj, std::forward<_T2>(__new_value));
45}
46#endif // _LIBCPP_STD_VER >= 14
47
48_LIBCPP_END_NAMESPACE_STD
49
50_LIBCPP_POP_MACROS
51
52#endif // _LIBCPP___UTILITY_EXCHANGE_H
53