[libc++] Workaround non-constexpr std::exchange pre C++20
authorLouis Dionne <ldionne.2@gmail.com>
Tue, 13 Jul 2021 14:48:54 +0000 (10:48 -0400)
committerLouis Dionne <ldionne.2@gmail.com>
Tue, 13 Jul 2021 14:51:03 +0000 (10:51 -0400)
std::exchange is only constexpr in C++20 and later. We were using it
in a constructor marked unconditionally constexpr, which caused issues
when building with -std=c++17.

The weird part is that the issue only showed up when building on the
arm64 macs, but that must be caused by the specific version of Clang
used on those. Since the code is clearly wrong and the fix is obvious,
I'm not going to investigate this further.

libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/explicit_optional_U.pass.cpp

index a591b5f..708370a 100644 (file)
@@ -48,7 +48,7 @@ class X
     int i_;
 public:
     constexpr explicit X(int i) : i_(i) {}
-    constexpr X(X&& x) : i_(std::exchange(x.i_, 0)) {}
+    constexpr X(X&& x) : i_(x.i_) { x.i_ = 0; }
     TEST_CONSTEXPR_CXX20 ~X() {i_ = 0;}
     friend constexpr bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
 };