diff options
Diffstat (limited to 'test/std/utilities/variant/variant.variant/variant.ctor/copy.pass.cpp')
-rw-r--r-- | test/std/utilities/variant/variant.variant/variant.ctor/copy.pass.cpp | 101 |
1 files changed, 96 insertions, 5 deletions
diff --git a/test/std/utilities/variant/variant.variant/variant.ctor/copy.pass.cpp b/test/std/utilities/variant/variant.variant/variant.ctor/copy.pass.cpp index 3b20c369c638..f3113435f3ce 100644 --- a/test/std/utilities/variant/variant.variant/variant.ctor/copy.pass.cpp +++ b/test/std/utilities/variant/variant.variant/variant.ctor/copy.pass.cpp @@ -51,6 +51,34 @@ struct MoveOnlyNT { MoveOnlyNT(MoveOnlyNT &&) {} }; +struct NTCopy { + constexpr NTCopy(int v) : value(v) {} + NTCopy(const NTCopy &that) : value(that.value) {} + NTCopy(NTCopy &&) = delete; + int value; +}; + +static_assert(!std::is_trivially_copy_constructible<NTCopy>::value, ""); +static_assert(std::is_copy_constructible<NTCopy>::value, ""); + +struct TCopy { + constexpr TCopy(int v) : value(v) {} + TCopy(TCopy const &) = default; + TCopy(TCopy &&) = delete; + int value; +}; + +static_assert(std::is_trivially_copy_constructible<TCopy>::value, ""); + +struct TCopyNTMove { + constexpr TCopyNTMove(int v) : value(v) {} + TCopyNTMove(const TCopyNTMove&) = default; + TCopyNTMove(TCopyNTMove&& that) : value(that.value) { that.value = -1; } + int value; +}; + +static_assert(std::is_trivially_copy_constructible<TCopyNTMove>::value, ""); + #ifndef TEST_HAS_NO_EXCEPTIONS struct MakeEmptyT { static int alive; @@ -71,7 +99,7 @@ int MakeEmptyT::alive = 0; template <class Variant> void makeEmpty(Variant &v) { Variant v2(std::in_place_type<MakeEmptyT>); try { - v = v2; + v = std::move(v2); assert(false); } catch (...) { assert(v.valueless_by_exception()); @@ -96,6 +124,25 @@ void test_copy_ctor_sfinae() { using V = std::variant<int, MoveOnlyNT>; static_assert(!std::is_copy_constructible<V>::value, ""); } + + // The following tests are for not-yet-standardized behavior (P0602): + { + using V = std::variant<int, long>; + static_assert(std::is_trivially_copy_constructible<V>::value, ""); + } + { + using V = std::variant<int, NTCopy>; + static_assert(!std::is_trivially_copy_constructible<V>::value, ""); + static_assert(std::is_copy_constructible<V>::value, ""); + } + { + using V = std::variant<int, TCopy>; + static_assert(std::is_trivially_copy_constructible<V>::value, ""); + } + { + using V = std::variant<int, TCopyNTMove>; + static_assert(std::is_trivially_copy_constructible<V>::value, ""); + } } void test_copy_ctor_basic() { @@ -125,6 +172,50 @@ void test_copy_ctor_basic() { assert(v2.index() == 1); assert(std::get<1>(v2).value == 42); } + + // The following tests are for not-yet-standardized behavior (P0602): + { + constexpr std::variant<int> v(std::in_place_index<0>, 42); + static_assert(v.index() == 0, ""); + constexpr std::variant<int> v2 = v; + static_assert(v2.index() == 0, ""); + static_assert(std::get<0>(v2) == 42, ""); + } + { + constexpr std::variant<int, long> v(std::in_place_index<1>, 42); + static_assert(v.index() == 1, ""); + constexpr std::variant<int, long> v2 = v; + static_assert(v2.index() == 1, ""); + static_assert(std::get<1>(v2) == 42, ""); + } + { + constexpr std::variant<TCopy> v(std::in_place_index<0>, 42); + static_assert(v.index() == 0, ""); + constexpr std::variant<TCopy> v2(v); + static_assert(v2.index() == 0, ""); + static_assert(std::get<0>(v2).value == 42, ""); + } + { + constexpr std::variant<int, TCopy> v(std::in_place_index<1>, 42); + static_assert(v.index() == 1, ""); + constexpr std::variant<int, TCopy> v2(v); + static_assert(v2.index() == 1, ""); + static_assert(std::get<1>(v2).value == 42, ""); + } + { + constexpr std::variant<TCopyNTMove> v(std::in_place_index<0>, 42); + static_assert(v.index() == 0, ""); + constexpr std::variant<TCopyNTMove> v2(v); + static_assert(v2.index() == 0, ""); + static_assert(std::get<0>(v2).value == 42, ""); + } + { + constexpr std::variant<int, TCopyNTMove> v(std::in_place_index<1>, 42); + static_assert(v.index() == 1, ""); + constexpr std::variant<int, TCopyNTMove> v2(v); + static_assert(v2.index() == 1, ""); + static_assert(std::get<1>(v2).value == 42, ""); + } } void test_copy_ctor_valueless_by_exception() { @@ -135,7 +226,7 @@ void test_copy_ctor_valueless_by_exception() { const V &cv1 = v1; V v(cv1); assert(v.valueless_by_exception()); -#endif +#endif // TEST_HAS_NO_EXCEPTIONS } template <size_t Idx> @@ -149,7 +240,7 @@ constexpr bool test_constexpr_copy_ctor_extension_imp( } void test_constexpr_copy_ctor_extension() { - // NOTE: This test is for not yet standardized behavior. + // NOTE: This test is for not yet standardized behavior. (P0602) using V = std::variant<long, void*, const int>; #ifdef TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE static_assert(std::is_trivially_destructible<V>::value, ""); @@ -157,9 +248,9 @@ void test_constexpr_copy_ctor_extension() { 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 +#else // TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE static_assert(std::is_trivially_copyable<V>::value, ""); -#endif +#endif // TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE static_assert(test_constexpr_copy_ctor_extension_imp<0>(V(42l)), ""); static_assert(test_constexpr_copy_ctor_extension_imp<1>(V(nullptr)), ""); static_assert(test_constexpr_copy_ctor_extension_imp<2>(V(101)), ""); |