[libcxx-commits] [libcxx] [libc++] Add `__exchange` as a C++11 utility (PR #187953)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Tue May 5 01:20:27 PDT 2026


================
@@ -24,14 +27,21 @@ _LIBCPP_PUSH_MACROS
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-#if _LIBCPP_STD_VER >= 14
 template <class _T1, class _T2 = _T1>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _T1 exchange(_T1& __obj, _T2&& __new_value) noexcept(
-    is_nothrow_move_constructible<_T1>::value && is_nothrow_assignable<_T1&, _T2>::value) {
+[[nodiscard]] _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _T1
----------------
Zingam wrote:

According to:
- https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
- https://eel.is/c++draft/utility.exchange
IMO omitting `[[nodiscard]]` earlier was an oversight. I don't remember any discussions that we shouldn't do it.

@rupprecht Are there any valid uses where we don't want to use the result, so that it would be a mistake to annotate `std::exchange`? IMO what we are missing here is the test to confirm this was intentional.

https://github.com/llvm/llvm-project/pull/187953


More information about the libcxx-commits mailing list