[libcxx-commits] [libcxx] [libc++] Make `constexpr std::variant`. Implement P2231R1 (PR #83335)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Feb 28 13:07:19 PST 2024
https://github.com/huixie90 created https://github.com/llvm/llvm-project/pull/83335
None
>From 9ba67d9e23cb4a1002cd703d4c0f516ca8bded07 Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Wed, 28 Feb 2024 21:06:51 +0000
Subject: [PATCH] [libc++] Make `constexpr std::variant`. Implement P2231R1
---
libcxx/include/variant | 93 ++++++++--------
.../variant.ctor/copy.pass.cpp | 90 ++++++++++------
.../variant.ctor/move.pass.cpp | 97 +++++++++++------
.../variant.dtor/dtor.pass.cpp | 71 ++++++++-----
.../variant.mod/emplace_index_args.pass.cpp | 93 ++++------------
.../emplace_index_init_list_args.pass.cpp | 26 +++--
.../variant.mod/emplace_type_args.pass.cpp | 100 +++++-------------
.../emplace_type_init_list_args.pass.cpp | 34 +++---
8 files changed, 299 insertions(+), 305 deletions(-)
diff --git a/libcxx/include/variant b/libcxx/include/variant
index 6063739e52c86b..895f2350449e65 100644
--- a/libcxx/include/variant
+++ b/libcxx/include/variant
@@ -42,7 +42,7 @@ namespace std {
in_place_index_t<I>, initializer_list<U>, Args&&...);
// 20.7.2.2, destructor
- ~variant();
+ constexpr ~variant(); // constexpr since c++20
// 20.7.2.3, assignment
constexpr variant& operator=(const variant&);
@@ -52,16 +52,16 @@ namespace std {
// 20.7.2.4, modifiers
template <class T, class... Args>
- T& emplace(Args&&...);
+ constexpr T& emplace(Args&&...); // constexpr since c++20
template <class T, class U, class... Args>
- T& emplace(initializer_list<U>, Args&&...);
+ constexpr T& emplace(initializer_list<U>, Args&&...); // constexpr since c++20
template <size_t I, class... Args>
- variant_alternative_t<I, variant>& emplace(Args&&...);
+ constexpr variant_alternative_t<I, variant>& emplace(Args&&...); // constexpr since c++20
template <size_t I, class U, class... Args>
- variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
+ constexpr variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...); // constexpr since c++20
// 20.7.2.5, value status
constexpr bool valueless_by_exception() const noexcept;
@@ -222,6 +222,7 @@ namespace std {
#include <__functional/operations.h>
#include <__functional/unary_function.h>
#include <__memory/addressof.h>
+#include <__memory/construct_at.h>
#include <__type_traits/add_const.h>
#include <__type_traits/add_cv.h>
#include <__type_traits/add_pointer.h>
@@ -655,7 +656,8 @@ private:
template <size_t _Index, class _Tp>
struct _LIBCPP_TEMPLATE_VIS __alt {
- using __value_type = _Tp;
+ using __value_type = _Tp;
+ static constexpr size_t __index = _Index;
template <class... _Args>
_LIBCPP_HIDE_FROM_ABI explicit constexpr __alt(in_place_t, _Args&&... __args)
@@ -701,9 +703,9 @@ union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
friend struct __access::__union; \
}
-_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
-_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union(){});
-_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
+_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() = default;);
+_LIBCPP_VARIANT_UNION(_Trait::_Available, _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union(){});
+_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() = delete;);
# undef _LIBCPP_VARIANT_UNION
@@ -767,13 +769,13 @@ class _LIBCPP_TEMPLATE_VIS __dtor;
}
_LIBCPP_VARIANT_DESTRUCTOR(
- _Trait::_TriviallyAvailable, ~__dtor() = default;
- , void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
+ _Trait::_TriviallyAvailable, _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() = default;
+ , _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
_LIBCPP_VARIANT_DESTRUCTOR(
_Trait::_Available,
- ~__dtor() { __destroy(); },
- void __destroy() noexcept {
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() { __destroy(); },
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept {
if (!this->valueless_by_exception()) {
__visitation::__base::__visit_alt(
[](auto& __alt) noexcept {
@@ -785,7 +787,8 @@ _LIBCPP_VARIANT_DESTRUCTOR(
this->__index = __variant_npos<__index_t>;
});
-_LIBCPP_VARIANT_DESTRUCTOR(_Trait::_Unavailable, ~__dtor() = delete;, void __destroy() noexcept = delete;);
+_LIBCPP_VARIANT_DESTRUCTOR(_Trait::_Unavailable, _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() = delete;
+ , _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept = delete;);
# undef _LIBCPP_VARIANT_DESTRUCTOR
@@ -798,23 +801,22 @@ public:
using __base_type::operator=;
protected:
- template <size_t _Ip, class _Tp, class... _Args>
- _LIBCPP_HIDE_FROM_ABI static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
- ::new ((void*)std::addressof(__a)) __alt<_Ip, _Tp>(in_place, std::forward<_Args>(__args)...);
- return __a.__value;
- }
-
template <class _Rhs>
- _LIBCPP_HIDE_FROM_ABI static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
__lhs.__destroy();
if (!__rhs.valueless_by_exception()) {
+ // We cannot directly construct at the target __alt because its direct enclosing union is not activated yet.
+ // We will get error if we did:
+ // construction of subobject of member '__tail' of union with active member '__dummy' is not allowed in a constant
+ // expression
auto __rhs_index = __rhs.index();
__visitation::__base::__visit_alt_at(
__rhs_index,
- [](auto& __lhs_alt, auto&& __rhs_alt) {
- __construct_alt(__lhs_alt, std::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
+ [&__lhs](auto&& __rhs_alt) {
+ std::__construct_at(std::addressof(__lhs.__data),
+ in_place_index<__decay_t<decltype(__rhs_alt)>::__index>,
+ std::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
},
- __lhs,
std::forward<_Rhs>(__rhs));
__lhs.__index = __rhs_index;
}
@@ -840,15 +842,18 @@ class _LIBCPP_TEMPLATE_VIS __move_constructor;
__move_constructor& operator=(__move_constructor&&) = default; \
}
-_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(_Trait::_TriviallyAvailable,
- __move_constructor(__move_constructor&& __that) = default;);
+_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
+ _Trait::_TriviallyAvailable,
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&& __that) = default;);
_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
_Trait::_Available,
- __move_constructor(__move_constructor&& __that) noexcept(__all<is_nothrow_move_constructible_v<_Types>...>::value)
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&& __that) noexcept(
+ __all<is_nothrow_move_constructible_v<_Types>...>::value)
: __move_constructor(__valueless_t{}) { this->__generic_construct(*this, std::move(__that)); });
-_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(_Trait::_Unavailable, __move_constructor(__move_constructor&&) = delete;);
+_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(_Trait::_Unavailable,
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&&) = delete;);
# undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
@@ -871,14 +876,16 @@ class _LIBCPP_TEMPLATE_VIS __copy_constructor;
__copy_constructor& operator=(__copy_constructor&&) = default; \
}
-_LIBCPP_VARIANT_COPY_CONSTRUCTOR(_Trait::_TriviallyAvailable,
- __copy_constructor(const __copy_constructor& __that) = default;);
+_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
+ _Trait::_TriviallyAvailable,
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor& __that) = default;);
_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
- _Trait::_Available, __copy_constructor(const __copy_constructor& __that)
+ _Trait::_Available, _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor& __that)
: __copy_constructor(__valueless_t{}) { this->__generic_construct(*this, __that); });
-_LIBCPP_VARIANT_COPY_CONSTRUCTOR(_Trait::_Unavailable, __copy_constructor(const __copy_constructor&) = delete;);
+_LIBCPP_VARIANT_COPY_CONSTRUCTOR(_Trait::_Unavailable,
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor&) = delete;);
# undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
@@ -891,22 +898,24 @@ public:
using __base_type::operator=;
template <size_t _Ip, class... _Args>
- _LIBCPP_HIDE_FROM_ABI auto& __emplace(_Args&&... __args) {
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI auto& __emplace(_Args&&... __args) {
this->__destroy();
- auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this), std::forward<_Args>(__args)...);
+ std::__construct_at(std::addressof(this->__data), in_place_index<_Ip>, std::forward<_Args>(__args)...);
this->__index = _Ip;
- return __res;
+ return __access::__base::__get_alt<_Ip>(*this).__value;
}
protected:
template <size_t _Ip, class _Tp, class _Arg>
- _LIBCPP_HIDE_FROM_ABI void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
if (this->index() == _Ip) {
__a.__value = std::forward<_Arg>(__arg);
} else {
struct {
- _LIBCPP_HIDE_FROM_ABI void operator()(true_type) const { __this->__emplace<_Ip>(std::forward<_Arg>(__arg)); }
- _LIBCPP_HIDE_FROM_ABI void operator()(false_type) const {
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void operator()(true_type) const {
+ __this->__emplace<_Ip>(std::forward<_Arg>(__arg));
+ }
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void operator()(false_type) const {
__this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg)));
}
__assignment* __this;
@@ -1248,7 +1257,7 @@ public:
enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
- _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&... __args) {
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&... __args) {
return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
}
@@ -1258,7 +1267,7 @@ public:
enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
- _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
}
@@ -1266,7 +1275,7 @@ public:
class... _Args,
size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
- _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&... __args) {
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&... __args) {
return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
}
@@ -1275,7 +1284,7 @@ public:
class... _Args,
size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
- _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
}
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.ctor/copy.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.ctor/copy.pass.cpp
index d1e5768f58d2b1..34626a4e25f355 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.ctor/copy.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.ctor/copy.pass.cpp
@@ -23,29 +23,29 @@
struct NonT {
NonT(int v) : value(v) {}
- NonT(const NonT &o) : value(o.value) {}
+ NonT(const NonT& o) : value(o.value) {}
int value;
};
static_assert(!std::is_trivially_copy_constructible<NonT>::value, "");
struct NoCopy {
- NoCopy(const NoCopy &) = delete;
+ NoCopy(const NoCopy&) = delete;
};
struct MoveOnly {
- MoveOnly(const MoveOnly &) = delete;
- MoveOnly(MoveOnly &&) = default;
+ MoveOnly(const MoveOnly&) = delete;
+ MoveOnly(MoveOnly&&) = default;
};
struct MoveOnlyNT {
- MoveOnlyNT(const MoveOnlyNT &) = delete;
- MoveOnlyNT(MoveOnlyNT &&) {}
+ MoveOnlyNT(const MoveOnlyNT&) = delete;
+ MoveOnlyNT(MoveOnlyNT&&) {}
};
struct NTCopy {
constexpr NTCopy(int v) : value(v) {}
- NTCopy(const NTCopy &that) : value(that.value) {}
- NTCopy(NTCopy &&) = delete;
+ NTCopy(const NTCopy& that) : value(that.value) {}
+ NTCopy(NTCopy&&) = delete;
int value;
};
@@ -54,8 +54,8 @@ static_assert(std::is_copy_constructible<NTCopy>::value, "");
struct TCopy {
constexpr TCopy(int v) : value(v) {}
- TCopy(TCopy const &) = default;
- TCopy(TCopy &&) = delete;
+ TCopy(TCopy const&) = default;
+ TCopy(TCopy&&) = delete;
int value;
};
@@ -74,20 +74,21 @@ static_assert(std::is_trivially_copy_constructible<TCopyNTMove>::value, "");
struct MakeEmptyT {
static int alive;
MakeEmptyT() { ++alive; }
- MakeEmptyT(const MakeEmptyT &) {
+ MakeEmptyT(const MakeEmptyT&) {
++alive;
// Don't throw from the copy constructor since variant's assignment
// operator performs a copy before committing to the assignment.
}
- MakeEmptyT(MakeEmptyT &&) { throw 42; }
- MakeEmptyT &operator=(const MakeEmptyT &) { throw 42; }
- MakeEmptyT &operator=(MakeEmptyT &&) { throw 42; }
+ MakeEmptyT(MakeEmptyT&&) { throw 42; }
+ MakeEmptyT& operator=(const MakeEmptyT&) { throw 42; }
+ MakeEmptyT& operator=(MakeEmptyT&&) { throw 42; }
~MakeEmptyT() { --alive; }
};
int MakeEmptyT::alive = 0;
-template <class Variant> void makeEmpty(Variant &v) {
+template <class Variant>
+void makeEmpty(Variant& v) {
Variant v2(std::in_place_type<MakeEmptyT>);
try {
v = std::move(v2);
@@ -214,35 +215,58 @@ void test_copy_ctor_valueless_by_exception() {
using V = std::variant<int, MakeEmptyT>;
V v1;
makeEmpty(v1);
- const V &cv1 = v1;
+ const V& cv1 = v1;
V v(cv1);
assert(v.valueless_by_exception());
#endif // TEST_HAS_NO_EXCEPTIONS
}
-template <std::size_t Idx>
-constexpr bool test_constexpr_copy_ctor_imp(std::variant<long, void*, const int> const& v) {
+template <std::size_t Idx, class T>
+constexpr bool test_constexpr_copy_ctor_imp(const T& v) {
auto v2 = v;
- return v2.index() == v.index() &&
- v2.index() == Idx &&
- std::get<Idx>(v2) == std::get<Idx>(v);
+ return v2.index() == v.index() && v2.index() == Idx && std::get<Idx>(v2) == std::get<Idx>(v);
}
+#if TEST_STD_VER >= 20
+struct NonTrivialCopyCtor {
+ int i = 0;
+ constexpr NonTrivialCopyCtor(int ii) : i(ii) {}
+ constexpr NonTrivialCopyCtor(const NonTrivialCopyCtor& other) : i(other.i) {}
+ constexpr NonTrivialCopyCtor(NonTrivialCopyCtor&& other) = default;
+ constexpr ~NonTrivialCopyCtor() = default;
+ friend constexpr bool operator==(const NonTrivialCopyCtor&, const NonTrivialCopyCtor&) = default;
+};
+#endif
void test_constexpr_copy_ctor() {
- // Make sure we properly propagate triviality, which implies constexpr-ness (see P0602R4).
- using V = std::variant<long, void*, const int>;
+ {
+ // Test is_trivially_copy_constructible
+ // Make sure we properly propagate triviality, which implies constexpr-ness (see P0602R4).
+ using V = std::variant<long, void*, const int>;
#ifdef TEST_WORKAROUND_MSVC_BROKEN_IS_TRIVIALLY_COPYABLE
- static_assert(std::is_trivially_destructible<V>::value, "");
- static_assert(std::is_trivially_copy_constructible<V>::value, "");
- static_assert(std::is_trivially_move_constructible<V>::value, "");
- static_assert(!std::is_copy_assignable<V>::value, "");
- static_assert(!std::is_move_assignable<V>::value, "");
-#else // TEST_WORKAROUND_MSVC_BROKEN_IS_TRIVIALLY_COPYABLE
- static_assert(std::is_trivially_copyable<V>::value, "");
+ static_assert(std::is_trivially_destructible<V>::value, "");
+ static_assert(std::is_trivially_copy_constructible<V>::value, "");
+ static_assert(std::is_trivially_move_constructible<V>::value, "");
+ static_assert(!std::is_copy_assignable<V>::value, "");
+ static_assert(!std::is_move_assignable<V>::value, "");
+#else // TEST_WORKAROUND_MSVC_BROKEN_IS_TRIVIALLY_COPYABLE
+ static_assert(std::is_trivially_copyable<V>::value, "");
#endif // TEST_WORKAROUND_MSVC_BROKEN_IS_TRIVIALLY_COPYABLE
- static_assert(test_constexpr_copy_ctor_imp<0>(V(42l)), "");
- static_assert(test_constexpr_copy_ctor_imp<1>(V(nullptr)), "");
- static_assert(test_constexpr_copy_ctor_imp<2>(V(101)), "");
+ static_assert(std::is_trivially_copy_constructible<V>::value, "");
+ static_assert(test_constexpr_copy_ctor_imp<0>(V(42l)), "");
+ static_assert(test_constexpr_copy_ctor_imp<1>(V(nullptr)), "");
+ static_assert(test_constexpr_copy_ctor_imp<2>(V(101)), "");
+ }
+
+#if TEST_STD_VER >= 20
+ {
+ // Test !is_trivially_move_constructible
+ using V = std::variant<long, NonTrivialCopyCtor, void*>;
+ static_assert(!std::is_trivially_copy_constructible<V>::value, "");
+ static_assert(test_constexpr_copy_ctor_imp<0>(V(42l)), "");
+ static_assert(test_constexpr_copy_ctor_imp<1>(V(NonTrivialCopyCtor(5))), "");
+ static_assert(test_constexpr_copy_ctor_imp<2>(V(nullptr)), "");
+ }
+#endif
}
int main(int, char**) {
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.ctor/move.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.ctor/move.pass.cpp
index e2518fe29caf7e..4bb6cf24b0f0e2 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.ctor/move.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.ctor/move.pass.cpp
@@ -23,31 +23,31 @@
#include "test_workarounds.h"
struct ThrowsMove {
- ThrowsMove(ThrowsMove &&) noexcept(false) {}
+ ThrowsMove(ThrowsMove&&) noexcept(false) {}
};
struct NoCopy {
- NoCopy(const NoCopy &) = delete;
+ NoCopy(const NoCopy&) = delete;
};
struct MoveOnly {
int value;
MoveOnly(int v) : value(v) {}
- MoveOnly(const MoveOnly &) = delete;
- MoveOnly(MoveOnly &&) = default;
+ MoveOnly(const MoveOnly&) = delete;
+ MoveOnly(MoveOnly&&) = default;
};
struct MoveOnlyNT {
int value;
MoveOnlyNT(int v) : value(v) {}
- MoveOnlyNT(const MoveOnlyNT &) = delete;
- MoveOnlyNT(MoveOnlyNT &&other) : value(other.value) { other.value = -1; }
+ MoveOnlyNT(const MoveOnlyNT&) = delete;
+ MoveOnlyNT(MoveOnlyNT&& other) : value(other.value) { other.value = -1; }
};
struct NTMove {
constexpr NTMove(int v) : value(v) {}
- NTMove(const NTMove &) = delete;
- NTMove(NTMove &&that) : value(that.value) { that.value = -1; }
+ NTMove(const NTMove&) = delete;
+ NTMove(NTMove&& that) : value(that.value) { that.value = -1; }
int value;
};
@@ -56,8 +56,8 @@ static_assert(std::is_move_constructible<NTMove>::value, "");
struct TMove {
constexpr TMove(int v) : value(v) {}
- TMove(const TMove &) = delete;
- TMove(TMove &&) = default;
+ TMove(const TMove&) = delete;
+ TMove(TMove&&) = default;
int value;
};
@@ -76,20 +76,21 @@ static_assert(std::is_trivially_move_constructible<TMoveNTCopy>::value, "");
struct MakeEmptyT {
static int alive;
MakeEmptyT() { ++alive; }
- MakeEmptyT(const MakeEmptyT &) {
+ MakeEmptyT(const MakeEmptyT&) {
++alive;
// Don't throw from the copy constructor since variant's assignment
// operator performs a copy before committing to the assignment.
}
- MakeEmptyT(MakeEmptyT &&) { throw 42; }
- MakeEmptyT &operator=(const MakeEmptyT &) { throw 42; }
- MakeEmptyT &operator=(MakeEmptyT &&) { throw 42; }
+ MakeEmptyT(MakeEmptyT&&) { throw 42; }
+ MakeEmptyT& operator=(const MakeEmptyT&) { throw 42; }
+ MakeEmptyT& operator=(MakeEmptyT&&) { throw 42; }
~MakeEmptyT() { --alive; }
};
int MakeEmptyT::alive = 0;
-template <class Variant> void makeEmpty(Variant &v) {
+template <class Variant>
+void makeEmpty(Variant& v) {
Variant v2(std::in_place_type<MakeEmptyT>);
try {
v = std::move(v2);
@@ -158,7 +159,10 @@ void test_move_ctor_sfinae() {
}
template <typename T>
-struct Result { std::size_t index; T value; };
+struct Result {
+ std::size_t index;
+ T value;
+};
void test_move_ctor_basic() {
{
@@ -289,31 +293,54 @@ void test_move_ctor_valueless_by_exception() {
#endif // TEST_HAS_NO_EXCEPTIONS
}
-template <std::size_t Idx>
-constexpr bool test_constexpr_ctor_imp(std::variant<long, void*, const int> const& v) {
+template <std::size_t Idx, class T>
+constexpr bool test_constexpr_ctor_imp(const T& v) {
auto copy = v;
- auto v2 = std::move(copy);
- return v2.index() == v.index() &&
- v2.index() == Idx &&
- std::get<Idx>(v2) == std::get<Idx>(v);
+ auto v2 = std::move(copy);
+ return v2.index() == v.index() && v2.index() == Idx && std::get<Idx>(v2) == std::get<Idx>(v);
}
+#if TEST_STD_VER >= 20
+struct NonTrivialMoveCtor {
+ int i = 0;
+ constexpr NonTrivialMoveCtor(int ii) : i(ii) {}
+ constexpr NonTrivialMoveCtor(const NonTrivialMoveCtor& other) = default;
+ constexpr NonTrivialMoveCtor(NonTrivialMoveCtor&& other) : i(other.i) {}
+ constexpr ~NonTrivialMoveCtor() = default;
+ friend constexpr bool operator==(const NonTrivialMoveCtor&, const NonTrivialMoveCtor&) = default;
+};
+#endif
+
void test_constexpr_move_ctor() {
- // Make sure we properly propagate triviality, which implies constexpr-ness (see P0602R4).
- using V = std::variant<long, void*, const int>;
+ {
+ // Test is_trivially_move_constructible
+ // Make sure we properly propagate triviality, which implies constexpr-ness (see P0602R4).
+ using V = std::variant<long, void*, const int>;
#ifdef TEST_WORKAROUND_MSVC_BROKEN_IS_TRIVIALLY_COPYABLE
- static_assert(std::is_trivially_destructible<V>::value, "");
- static_assert(std::is_trivially_copy_constructible<V>::value, "");
- static_assert(std::is_trivially_move_constructible<V>::value, "");
- static_assert(!std::is_copy_assignable<V>::value, "");
- static_assert(!std::is_move_assignable<V>::value, "");
-#else // TEST_WORKAROUND_MSVC_BROKEN_IS_TRIVIALLY_COPYABLE
- static_assert(std::is_trivially_copyable<V>::value, "");
+ static_assert(std::is_trivially_destructible<V>::value, "");
+ static_assert(std::is_trivially_copy_constructible<V>::value, "");
+ static_assert(std::is_trivially_move_constructible<V>::value, "");
+ static_assert(!std::is_copy_assignable<V>::value, "");
+ static_assert(!std::is_move_assignable<V>::value, "");
+#else // TEST_WORKAROUND_MSVC_BROKEN_IS_TRIVIALLY_COPYABLE
+ static_assert(std::is_trivially_copyable<V>::value, "");
#endif // TEST_WORKAROUND_MSVC_BROKEN_IS_TRIVIALLY_COPYABLE
- static_assert(std::is_trivially_move_constructible<V>::value, "");
- static_assert(test_constexpr_ctor_imp<0>(V(42l)), "");
- static_assert(test_constexpr_ctor_imp<1>(V(nullptr)), "");
- static_assert(test_constexpr_ctor_imp<2>(V(101)), "");
+ static_assert(std::is_trivially_move_constructible<V>::value, "");
+ static_assert(test_constexpr_ctor_imp<0>(V(42l)), "");
+ static_assert(test_constexpr_ctor_imp<1>(V(nullptr)), "");
+ static_assert(test_constexpr_ctor_imp<2>(V(101)), "");
+ }
+
+#if TEST_STD_VER >= 20
+ {
+ // Test !is_trivially_move_constructible
+ using V = std::variant<long, NonTrivialMoveCtor, void*>;
+ static_assert(!std::is_trivially_move_constructible<V>::value, "");
+ static_assert(test_constexpr_ctor_imp<0>(V(42l)), "");
+ static_assert(test_constexpr_ctor_imp<1>(V(NonTrivialMoveCtor(5))), "");
+ static_assert(test_constexpr_ctor_imp<2>(V(nullptr)), "");
+ }
+#endif
}
int main(int, char**) {
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.dtor/dtor.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.dtor/dtor.pass.cpp
index 2e026038c97a3c..53c5283b2edc6f 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.dtor/dtor.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.dtor/dtor.pass.cpp
@@ -21,55 +21,76 @@
#include "test_macros.h"
struct NonTDtor {
- static int count;
- NonTDtor() = default;
- ~NonTDtor() { ++count; }
+ int* count;
+ constexpr NonTDtor(int* a, int*) : count(a) {}
+ TEST_CONSTEXPR_CXX20 ~NonTDtor() { ++*count; }
};
-int NonTDtor::count = 0;
static_assert(!std::is_trivially_destructible<NonTDtor>::value, "");
struct NonTDtor1 {
- static int count;
- NonTDtor1() = default;
- ~NonTDtor1() { ++count; }
+ int* count;
+ constexpr NonTDtor1(int*, int* b) : count(b) {}
+ TEST_CONSTEXPR_CXX20 ~NonTDtor1() { ++*count; }
};
-int NonTDtor1::count = 0;
static_assert(!std::is_trivially_destructible<NonTDtor1>::value, "");
struct TDtor {
- TDtor(const TDtor &) {} // non-trivial copy
- ~TDtor() = default;
+ constexpr TDtor() = default;
+ constexpr TDtor(const TDtor&) {} // non-trivial copy
+ TEST_CONSTEXPR_CXX20 ~TDtor() = default;
};
static_assert(!std::is_trivially_copy_constructible<TDtor>::value, "");
static_assert(std::is_trivially_destructible<TDtor>::value, "");
-int main(int, char**) {
+TEST_CONSTEXPR_CXX20 bool test() {
{
using V = std::variant<int, long, TDtor>;
static_assert(std::is_trivially_destructible<V>::value, "");
+ [[maybe_unused]] V v(std::in_place_index<2>);
}
{
using V = std::variant<NonTDtor, int, NonTDtor1>;
static_assert(!std::is_trivially_destructible<V>::value, "");
{
- V v(std::in_place_index<0>);
- assert(NonTDtor::count == 0);
- assert(NonTDtor1::count == 0);
+ int count0 = 0;
+ int count1 = 0;
+ {
+ V v(std::in_place_index<0>, &count0, &count1);
+ assert(count0 == 0);
+ assert(count1 == 0);
+ }
+ assert(count0 == 1);
+ assert(count1 == 0);
+ }
+ {
+ int count0 = 0;
+ int count1 = 0;
+ { V v(std::in_place_index<1>); }
+ assert(count0 == 0);
+ assert(count1 == 0);
}
- assert(NonTDtor::count == 1);
- assert(NonTDtor1::count == 0);
- NonTDtor::count = 0;
- { V v(std::in_place_index<1>); }
- assert(NonTDtor::count == 0);
- assert(NonTDtor1::count == 0);
{
- V v(std::in_place_index<2>);
- assert(NonTDtor::count == 0);
- assert(NonTDtor1::count == 0);
+ int count0 = 0;
+ int count1 = 0;
+ {
+ V v(std::in_place_index<2>, &count0, &count1);
+ assert(count0 == 0);
+ assert(count1 == 0);
+ }
+ assert(count0 == 0);
+ assert(count1 == 1);
}
- assert(NonTDtor::count == 0);
- assert(NonTDtor1::count == 1);
}
+ return true;
+}
+
+int main(int, char**) {
+ test();
+
+#if TEST_STD_VER >= 20
+ static_assert(test());
+#endif
+
return 0;
}
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_args.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_args.pass.cpp
index 96fcd7e7bee481..36c78d1b22bd0c 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_args.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_args.pass.cpp
@@ -26,8 +26,8 @@
#include "variant_test_helpers.h"
template <class Var, std::size_t I, class... Args>
-constexpr auto test_emplace_exists_imp(int) -> decltype(
- std::declval<Var>().template emplace<I>(std::declval<Args>()...), true) {
+constexpr auto test_emplace_exists_imp(int)
+ -> decltype(std::declval<Var>().template emplace<I>(std::declval<Args>()...), true) {
return true;
}
@@ -36,51 +36,32 @@ constexpr auto test_emplace_exists_imp(long) -> bool {
return false;
}
-template <class Var, std::size_t I, class... Args> constexpr bool emplace_exists() {
+template <class Var, std::size_t I, class... Args>
+constexpr bool emplace_exists() {
return test_emplace_exists_imp<Var, I, Args...>(0);
}
void test_emplace_sfinae() {
{
- using V = std::variant<int, void *, const void *, TestTypes::NoCtors>;
+ using V = std::variant<int, void*, const void*, TestTypes::NoCtors>;
static_assert(emplace_exists<V, 0>(), "");
static_assert(emplace_exists<V, 0, int>(), "");
- static_assert(!emplace_exists<V, 0, decltype(nullptr)>(),
- "cannot construct");
+ static_assert(!emplace_exists<V, 0, decltype(nullptr)>(), "cannot construct");
static_assert(emplace_exists<V, 1, decltype(nullptr)>(), "");
- static_assert(emplace_exists<V, 1, int *>(), "");
- static_assert(!emplace_exists<V, 1, const int *>(), "");
+ static_assert(emplace_exists<V, 1, int*>(), "");
+ static_assert(!emplace_exists<V, 1, const int*>(), "");
static_assert(!emplace_exists<V, 1, int>(), "cannot construct");
- static_assert(emplace_exists<V, 2, const int *>(), "");
- static_assert(emplace_exists<V, 2, int *>(), "");
+ static_assert(emplace_exists<V, 2, const int*>(), "");
+ static_assert(emplace_exists<V, 2, int*>(), "");
static_assert(!emplace_exists<V, 3>(), "cannot construct");
}
-#if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
- {
- using V = std::variant<int, int &, const int &, int &&, TestTypes::NoCtors>;
- static_assert(emplace_exists<V, 0>(), "");
- static_assert(emplace_exists<V, 0, int>(), "");
- static_assert(emplace_exists<V, 0, long long>(), "");
- static_assert(!emplace_exists<V, 0, int, int>(), "too many args");
- static_assert(emplace_exists<V, 1, int &>(), "");
- static_assert(!emplace_exists<V, 1>(), "cannot default construct ref");
- static_assert(!emplace_exists<V, 1, const int &>(), "cannot bind ref");
- static_assert(!emplace_exists<V, 1, int &&>(), "cannot bind ref");
- static_assert(emplace_exists<V, 2, int &>(), "");
- static_assert(emplace_exists<V, 2, const int &>(), "");
- static_assert(emplace_exists<V, 2, int &&>(), "");
- static_assert(!emplace_exists<V, 2, void *>(),
- "not constructible from void*");
- static_assert(emplace_exists<V, 3, int>(), "");
- static_assert(!emplace_exists<V, 3, int &>(), "cannot bind ref");
- static_assert(!emplace_exists<V, 3, const int &>(), "cannot bind ref");
- static_assert(!emplace_exists<V, 3, const int &&>(), "cannot bind ref");
- static_assert(!emplace_exists<V, 4>(), "no ctors");
- }
-#endif
}
-void test_basic() {
+struct NoCtor {
+ NoCtor() = delete;
+};
+
+TEST_CONSTEXPR_CXX20 bool test_basic() {
{
using V = std::variant<int>;
V v(42);
@@ -93,9 +74,9 @@ void test_basic() {
assert(std::get<0>(v) == 42);
assert(&ref2 == &std::get<0>(v));
}
+
{
- using V =
- std::variant<int, long, const void *, TestTypes::NoCtors, std::string>;
+ using V = std::variant<int, long, const void*, NoCtor, std::string>;
const int x = 100;
V v(std::in_place_index<0>, -1);
// default emplace a value
@@ -113,45 +94,15 @@ void test_basic() {
assert(std::get<4>(v) == "aaa");
assert(&ref3 == &std::get<4>(v));
}
-#if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
- {
- using V = std::variant<int, long, const int &, int &&, TestTypes::NoCtors,
- std::string>;
- const int x = 100;
- int y = 42;
- int z = 43;
- V v(std::in_place_index<0>, -1);
- // default emplace a value
- auto& ref1 = v.emplace<1>();
- static_assert(std::is_same_v<long&, decltype(ref1)>, "");
- assert(std::get<1>(v) == 0);
- assert(&ref1 == &std::get<1>(v));
- // emplace a reference
- auto& ref2 = v.emplace<2>(x);
- static_assert(std::is_same_v<&, decltype(ref)>, "");
- assert(&std::get<2>(v) == &x);
- assert(&ref2 == &std::get<2>(v));
- // emplace an rvalue reference
- auto& ref3 = v.emplace<3>(std::move(y));
- static_assert(std::is_same_v<&, decltype(ref)>, "");
- assert(&std::get<3>(v) == &y);
- assert(&ref3 == &std::get<3>(v));
- // re-emplace a new reference over the active member
- auto& ref4 = v.emplace<3>(std::move(z));
- static_assert(std::is_same_v<&, decltype(ref)>, "");
- assert(&std::get<3>(v) == &z);
- assert(&ref4 == &std::get<3>(v));
- // emplace with multiple args
- auto& ref5 = v.emplace<5>(3u, 'a');
- static_assert(std::is_same_v<std::string&, decltype(ref5)>, "");
- assert(std::get<5>(v) == "aaa");
- assert(&ref5 == &std::get<5>(v));
- }
-#endif
+
+ return true;
}
int main(int, char**) {
test_basic();
+#if TEST_STD_VER >= 20
+ static_assert(test_basic());
+#endif
test_emplace_sfinae();
return 0;
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_init_list_args.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_init_list_args.pass.cpp
index 9068aacc435928..0bf279f41a8e52 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_init_list_args.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_init_list_args.pass.cpp
@@ -32,13 +32,12 @@ struct InitList {
struct InitListArg {
std::size_t size;
int value;
- constexpr InitListArg(std::initializer_list<int> il, int v)
- : size(il.size()), value(v) {}
+ constexpr InitListArg(std::initializer_list<int> il, int v) : size(il.size()), value(v) {}
};
template <class Var, std::size_t I, class... Args>
-constexpr auto test_emplace_exists_imp(int) -> decltype(
- std::declval<Var>().template emplace<I>(std::declval<Args>()...), true) {
+constexpr auto test_emplace_exists_imp(int)
+ -> decltype(std::declval<Var>().template emplace<I>(std::declval<Args>()...), true) {
return true;
}
@@ -47,13 +46,13 @@ constexpr auto test_emplace_exists_imp(long) -> bool {
return false;
}
-template <class Var, std::size_t I, class... Args> constexpr bool emplace_exists() {
+template <class Var, std::size_t I, class... Args>
+constexpr bool emplace_exists() {
return test_emplace_exists_imp<Var, I, Args...>(0);
}
void test_emplace_sfinae() {
- using V =
- std::variant<int, TestTypes::NoCtors, InitList, InitListArg, long, long>;
+ using V = std::variant<int, TestTypes::NoCtors, InitList, InitListArg, long, long>;
using IL = std::initializer_list<int>;
static_assert(!emplace_exists<V, 1, IL>(), "no such constructor");
static_assert(emplace_exists<V, 2, IL>(), "");
@@ -65,8 +64,12 @@ void test_emplace_sfinae() {
static_assert(!emplace_exists<V, 3, IL, int, int>(), "too many args");
}
-void test_basic() {
- using V = std::variant<int, InitList, InitListArg, TestTypes::NoCtors>;
+struct NoCtor {
+ NoCtor() = delete;
+};
+
+TEST_CONSTEXPR_CXX20 bool test_basic() {
+ using V = std::variant<int, InitList, InitListArg, NoCtor>;
V v;
auto& ref1 = v.emplace<1>({1, 2, 3});
static_assert(std::is_same_v<InitList&, decltype(ref1)>, "");
@@ -81,10 +84,15 @@ void test_basic() {
static_assert(std::is_same_v<InitList&, decltype(ref3)>, "");
assert(std::get<1>(v).size == 1);
assert(&ref3 == &std::get<1>(v));
+
+ return true;
}
int main(int, char**) {
test_basic();
+#if TEST_STD_VER >= 20
+ static_assert(test_basic());
+#endif
test_emplace_sfinae();
return 0;
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_args.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_args.pass.cpp
index 24305aa0a35dae..7712a8149aa116 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_args.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_args.pass.cpp
@@ -25,8 +25,8 @@
#include "variant_test_helpers.h"
template <class Var, class T, class... Args>
-constexpr auto test_emplace_exists_imp(int) -> decltype(
- std::declval<Var>().template emplace<T>(std::declval<Args>()...), true) {
+constexpr auto test_emplace_exists_imp(int)
+ -> decltype(std::declval<Var>().template emplace<T>(std::declval<Args>()...), true) {
return true;
}
@@ -35,52 +35,32 @@ constexpr auto test_emplace_exists_imp(long) -> bool {
return false;
}
-template <class... Args> constexpr bool emplace_exists() {
+template <class... Args>
+constexpr bool emplace_exists() {
return test_emplace_exists_imp<Args...>(0);
}
void test_emplace_sfinae() {
{
- using V = std::variant<int, void *, const void *, TestTypes::NoCtors>;
+ using V = std::variant<int, void*, const void*, TestTypes::NoCtors>;
static_assert(emplace_exists<V, int>(), "");
static_assert(emplace_exists<V, int, int>(), "");
- static_assert(!emplace_exists<V, int, decltype(nullptr)>(),
- "cannot construct");
- static_assert(emplace_exists<V, void *, decltype(nullptr)>(), "");
- static_assert(!emplace_exists<V, void *, int>(), "cannot construct");
- static_assert(emplace_exists<V, void *, int *>(), "");
- static_assert(!emplace_exists<V, void *, const int *>(), "");
- static_assert(emplace_exists<V, const void *, const int *>(), "");
- static_assert(emplace_exists<V, const void *, int *>(), "");
+ static_assert(!emplace_exists<V, int, decltype(nullptr)>(), "cannot construct");
+ static_assert(emplace_exists<V, void*, decltype(nullptr)>(), "");
+ static_assert(!emplace_exists<V, void*, int>(), "cannot construct");
+ static_assert(emplace_exists<V, void*, int*>(), "");
+ static_assert(!emplace_exists<V, void*, const int*>(), "");
+ static_assert(emplace_exists<V, const void*, const int*>(), "");
+ static_assert(emplace_exists<V, const void*, int*>(), "");
static_assert(!emplace_exists<V, TestTypes::NoCtors>(), "cannot construct");
}
-#if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
- using V = std::variant<int, int &, const int &, int &&, long, long,
- TestTypes::NoCtors>;
- static_assert(emplace_exists<V, int>(), "");
- static_assert(emplace_exists<V, int, int>(), "");
- static_assert(emplace_exists<V, int, long long>(), "");
- static_assert(!emplace_exists<V, int, int, int>(), "too many args");
- static_assert(emplace_exists<V, int &, int &>(), "");
- static_assert(!emplace_exists<V, int &>(), "cannot default construct ref");
- static_assert(!emplace_exists<V, int &, const int &>(), "cannot bind ref");
- static_assert(!emplace_exists<V, int &, int &&>(), "cannot bind ref");
- static_assert(emplace_exists<V, const int &, int &>(), "");
- static_assert(emplace_exists<V, const int &, const int &>(), "");
- static_assert(emplace_exists<V, const int &, int &&>(), "");
- static_assert(!emplace_exists<V, const int &, void *>(),
- "not constructible from void*");
- static_assert(emplace_exists<V, int &&, int>(), "");
- static_assert(!emplace_exists<V, int &&, int &>(), "cannot bind ref");
- static_assert(!emplace_exists<V, int &&, const int &>(), "cannot bind ref");
- static_assert(!emplace_exists<V, int &&, const int &&>(), "cannot bind ref");
- static_assert(!emplace_exists<V, long, long>(), "ambiguous");
- static_assert(!emplace_exists<V, TestTypes::NoCtors>(),
- "cannot construct void");
-#endif
}
-void test_basic() {
+struct NoCtor {
+ NoCtor() = delete;
+};
+
+TEST_CONSTEXPR_CXX20 bool test_basic() {
{
using V = std::variant<int>;
V v(42);
@@ -94,8 +74,7 @@ void test_basic() {
assert(&ref2 == &std::get<0>(v));
}
{
- using V =
- std::variant<int, long, const void *, TestTypes::NoCtors, std::string>;
+ using V = std::variant<int, long, const void*, NoCtor, std::string>;
const int x = 100;
V v(std::in_place_type<int>, -1);
// default emplace a value
@@ -103,8 +82,8 @@ void test_basic() {
static_assert(std::is_same_v<long&, decltype(ref1)>, "");
assert(std::get<1>(v) == 0);
assert(&ref1 == &std::get<1>(v));
- auto& ref2 = v.emplace<const void *>(&x);
- static_assert(std::is_same_v<const void *&, decltype(ref2)>, "");
+ auto& ref2 = v.emplace<const void*>(&x);
+ static_assert(std::is_same_v<const void*&, decltype(ref2)>, "");
assert(std::get<2>(v) == &x);
assert(&ref2 == &std::get<2>(v));
// emplace with multiple args
@@ -113,45 +92,14 @@ void test_basic() {
assert(std::get<4>(v) == "aaa");
assert(&ref3 == &std::get<4>(v));
}
-#if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
- {
- using V = std::variant<int, long, const int &, int &&, TestTypes::NoCtors,
- std::string>;
- const int x = 100;
- int y = 42;
- int z = 43;
- V v(std::in_place_index<0>, -1);
- // default emplace a value
- auto& ref1 = v.emplace<long>();
- static_assert(std::is_same_v<long&, decltype(ref1)>, "");
- assert(std::get<long>(v) == 0);
- assert(&ref1 == &std::get<long>(v));
- // emplace a reference
- auto& ref2 = v.emplace<const int &>(x);
- static_assert(std::is_same_v<const int&, decltype(ref2)>, "");
- assert(&std::get<const int &>(v) == &x);
- assert(&ref2 == &std::get<const int &>(v));
- // emplace an rvalue reference
- auto& ref3 = v.emplace<int &&>(std::move(y));
- static_assert(std::is_same_v<int &&, decltype(ref3)>, "");
- assert(&std::get<int &&>(v) == &y);
- assert(&ref3 == &std::get<int &&>(v));
- // re-emplace a new reference over the active member
- auto& ref4 = v.emplace<int &&>(std::move(z));
- static_assert(std::is_same_v<int &, decltype(ref4)>, "");
- assert(&std::get<int &&>(v) == &z);
- assert(&ref4 == &std::get<int &&>(v));
- // emplace with multiple args
- auto& ref5 = v.emplace<std::string>(3u, 'a');
- static_assert(std::is_same_v<std::string&, decltype(ref5)>, "");
- assert(std::get<std::string>(v) == "aaa");
- assert(&ref5 == &std::get<std::string>(v));
- }
-#endif
+ return true;
}
int main(int, char**) {
test_basic();
+#if TEST_STD_VER >= 20
+ static_assert(test_basic());
+#endif
test_emplace_sfinae();
return 0;
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_init_list_args.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_init_list_args.pass.cpp
index 74d834b9b34575..6cd862c7d8095b 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_init_list_args.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_init_list_args.pass.cpp
@@ -32,13 +32,12 @@ struct InitList {
struct InitListArg {
std::size_t size;
int value;
- constexpr InitListArg(std::initializer_list<int> il, int v)
- : size(il.size()), value(v) {}
+ constexpr InitListArg(std::initializer_list<int> il, int v) : size(il.size()), value(v) {}
};
template <class Var, class T, class... Args>
-constexpr auto test_emplace_exists_imp(int) -> decltype(
- std::declval<Var>().template emplace<T>(std::declval<Args>()...), true) {
+constexpr auto test_emplace_exists_imp(int)
+ -> decltype(std::declval<Var>().template emplace<T>(std::declval<Args>()...), true) {
return true;
}
@@ -47,13 +46,13 @@ constexpr auto test_emplace_exists_imp(long) -> bool {
return false;
}
-template <class... Args> constexpr bool emplace_exists() {
+template <class... Args>
+constexpr bool emplace_exists() {
return test_emplace_exists_imp<Args...>(0);
}
void test_emplace_sfinae() {
- using V =
- std::variant<int, TestTypes::NoCtors, InitList, InitListArg, long, long>;
+ using V = std::variant<int, TestTypes::NoCtors, InitList, InitListArg, long, long>;
using IL = std::initializer_list<int>;
static_assert(emplace_exists<V, InitList, IL>(), "");
static_assert(!emplace_exists<V, InitList, int>(), "args don't match");
@@ -61,30 +60,37 @@ void test_emplace_sfinae() {
static_assert(emplace_exists<V, InitListArg, IL, int>(), "");
static_assert(!emplace_exists<V, InitListArg, int>(), "args don't match");
static_assert(!emplace_exists<V, InitListArg, IL>(), "too few args");
- static_assert(!emplace_exists<V, InitListArg, IL, int, int>(),
- "too many args");
+ static_assert(!emplace_exists<V, InitListArg, IL, int, int>(), "too many args");
}
-void test_basic() {
- using V = std::variant<int, InitList, InitListArg, TestTypes::NoCtors>;
+struct NoCtor {
+ NoCtor() = delete;
+};
+
+TEST_CONSTEXPR_CXX20 bool test_basic() {
+ using V = std::variant<int, InitList, InitListArg, NoCtor>;
V v;
auto& ref1 = v.emplace<InitList>({1, 2, 3});
- static_assert(std::is_same_v<InitList&,decltype(ref1)>, "");
+ static_assert(std::is_same_v<InitList&, decltype(ref1)>, "");
assert(std::get<InitList>(v).size == 3);
assert(&ref1 == &std::get<InitList>(v));
auto& ref2 = v.emplace<InitListArg>({1, 2, 3, 4}, 42);
- static_assert(std::is_same_v<InitListArg&,decltype(ref2)>, "");
+ static_assert(std::is_same_v<InitListArg&, decltype(ref2)>, "");
assert(std::get<InitListArg>(v).size == 4);
assert(std::get<InitListArg>(v).value == 42);
assert(&ref2 == &std::get<InitListArg>(v));
auto& ref3 = v.emplace<InitList>({1});
- static_assert(std::is_same_v<InitList&,decltype(ref3)>, "");
+ static_assert(std::is_same_v<InitList&, decltype(ref3)>, "");
assert(std::get<InitList>(v).size == 1);
assert(&ref3 == &std::get<InitList>(v));
+ return true;
}
int main(int, char**) {
test_basic();
+#if TEST_STD_VER >= 20
+ static_assert(test_basic());
+#endif
test_emplace_sfinae();
return 0;
More information about the libcxx-commits
mailing list