// -*- C++ -*- //===----------------------------------------------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++98, c++03, c++11, c++14 // XFAIL: with_system_cxx_lib=macosx10.12 // XFAIL: with_system_cxx_lib=macosx10.11 // XFAIL: with_system_cxx_lib=macosx10.10 // XFAIL: with_system_cxx_lib=macosx10.9 // XFAIL: with_system_cxx_lib=macosx10.7 // XFAIL: with_system_cxx_lib=macosx10.8 // // template class variant; // variant(variant const&); #include #include #include #include "test_macros.h" #include "test_workarounds.h" struct NonT { NonT(int v) : value(v) {} NonT(const NonT &o) : value(o.value) {} int value; }; static_assert(!std::is_trivially_copy_constructible::value, ""); struct NoCopy { NoCopy(const NoCopy &) = delete; }; struct MoveOnly { MoveOnly(const MoveOnly &) = delete; MoveOnly(MoveOnly &&) = default; }; struct 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; int value; }; static_assert(!std::is_trivially_copy_constructible::value, ""); static_assert(std::is_copy_constructible::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::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::value, ""); #ifndef TEST_HAS_NO_EXCEPTIONS struct MakeEmptyT { static int alive; MakeEmptyT() { ++alive; } 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() { --alive; } }; int MakeEmptyT::alive = 0; template void makeEmpty(Variant &v) { Variant v2(std::in_place_type); try { v = std::move(v2); assert(false); } catch (...) { assert(v.valueless_by_exception()); } } #endif // TEST_HAS_NO_EXCEPTIONS void test_copy_ctor_sfinae() { { using V = std::variant; static_assert(std::is_copy_constructible::value, ""); } { using V = std::variant; static_assert(!std::is_copy_constructible::value, ""); } { using V = std::variant; static_assert(!std::is_copy_constructible::value, ""); } { using V = std::variant; static_assert(!std::is_copy_constructible::value, ""); } // The following tests are for not-yet-standardized behavior (P0602): { using V = std::variant; static_assert(std::is_trivially_copy_constructible::value, ""); } { using V = std::variant; static_assert(!std::is_trivially_copy_constructible::value, ""); static_assert(std::is_copy_constructible::value, ""); } { using V = std::variant; static_assert(std::is_trivially_copy_constructible::value, ""); } { using V = std::variant; static_assert(std::is_trivially_copy_constructible::value, ""); } } void test_copy_ctor_basic() { { std::variant v(std::in_place_index<0>, 42); std::variant v2 = v; assert(v2.index() == 0); assert(std::get<0>(v2) == 42); } { std::variant v(std::in_place_index<1>, 42); std::variant v2 = v; assert(v2.index() == 1); assert(std::get<1>(v2) == 42); } { std::variant v(std::in_place_index<0>, 42); assert(v.index() == 0); std::variant v2(v); assert(v2.index() == 0); assert(std::get<0>(v2).value == 42); } { std::variant v(std::in_place_index<1>, 42); assert(v.index() == 1); std::variant v2(v); assert(v2.index() == 1); assert(std::get<1>(v2).value == 42); } // The following tests are for not-yet-standardized behavior (P0602): { constexpr std::variant v(std::in_place_index<0>, 42); static_assert(v.index() == 0, ""); constexpr std::variant v2 = v; static_assert(v2.index() == 0, ""); static_assert(std::get<0>(v2) == 42, ""); } { constexpr std::variant v(std::in_place_index<1>, 42); static_assert(v.index() == 1, ""); constexpr std::variant v2 = v; static_assert(v2.index() == 1, ""); static_assert(std::get<1>(v2) == 42, ""); } { constexpr std::variant v(std::in_place_index<0>, 42); static_assert(v.index() == 0, ""); constexpr std::variant v2(v); static_assert(v2.index() == 0, ""); static_assert(std::get<0>(v2).value == 42, ""); } { constexpr std::variant v(std::in_place_index<1>, 42); static_assert(v.index() == 1, ""); constexpr std::variant v2(v); static_assert(v2.index() == 1, ""); static_assert(std::get<1>(v2).value == 42, ""); } { constexpr std::variant v(std::in_place_index<0>, 42); static_assert(v.index() == 0, ""); constexpr std::variant v2(v); static_assert(v2.index() == 0, ""); static_assert(std::get<0>(v2).value == 42, ""); } { constexpr std::variant v(std::in_place_index<1>, 42); static_assert(v.index() == 1, ""); constexpr std::variant v2(v); static_assert(v2.index() == 1, ""); static_assert(std::get<1>(v2).value == 42, ""); } } void test_copy_ctor_valueless_by_exception() { #ifndef TEST_HAS_NO_EXCEPTIONS using V = std::variant; V v1; makeEmpty(v1); const V &cv1 = v1; V v(cv1); assert(v.valueless_by_exception()); #endif // TEST_HAS_NO_EXCEPTIONS } template constexpr bool test_constexpr_copy_ctor_extension_imp( std::variant const& v) { auto v2 = v; return v2.index() == v.index() && v2.index() == Idx && std::get(v2) == std::get(v); } void test_constexpr_copy_ctor_extension() { // NOTE: This test is for not yet standardized behavior. (P0602) using V = std::variant; #ifdef TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE static_assert(std::is_trivially_destructible::value, ""); static_assert(std::is_trivially_copy_constructible::value, ""); static_assert(std::is_trivially_move_constructible::value, ""); static_assert(!std::is_copy_assignable::value, ""); static_assert(!std::is_move_assignable::value, ""); #else // TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE static_assert(std::is_trivially_copyable::value, ""); #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)), ""); } int main() { test_copy_ctor_basic(); test_copy_ctor_valueless_by_exception(); test_copy_ctor_sfinae(); test_constexpr_copy_ctor_extension(); }