123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- // -*- 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
- // <variant>
- // template <class ...Types> class variant;
- // variant(variant const&);
- #include <cassert>
- #include <type_traits>
- #include <variant>
- #include "test_macros.h"
- struct NonT {
- NonT(int v) : value(v) {}
- NonT(NonT const &o) : value(o.value) {}
- int value;
- };
- static_assert(!std::is_trivially_copy_constructible<NonT>::value, "");
- struct NoCopy {
- NoCopy(NoCopy const &) = delete;
- };
- struct MoveOnly {
- MoveOnly(MoveOnly const &) = delete;
- MoveOnly(MoveOnly &&) = default;
- };
- struct MoveOnlyNT {
- MoveOnlyNT(MoveOnlyNT const &) = delete;
- MoveOnlyNT(MoveOnlyNT &&) {}
- };
- #ifndef TEST_HAS_NO_EXCEPTIONS
- struct MakeEmptyT {
- static int alive;
- MakeEmptyT() { ++alive; }
- MakeEmptyT(MakeEmptyT const &) {
- ++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=(MakeEmptyT const &) { throw 42; }
- MakeEmptyT &operator=(MakeEmptyT &&) { throw 42; }
- ~MakeEmptyT() { --alive; }
- };
- int MakeEmptyT::alive = 0;
- template <class Variant> void makeEmpty(Variant &v) {
- Variant v2(std::in_place_type<MakeEmptyT>);
- try {
- v = v2;
- assert(false);
- } catch (...) {
- assert(v.valueless_by_exception());
- }
- }
- #endif // TEST_HAS_NO_EXCEPTIONS
- void test_copy_ctor_sfinae() {
- {
- using V = std::variant<int, long>;
- static_assert(std::is_copy_constructible<V>::value, "");
- }
- {
- using V = std::variant<int, NoCopy>;
- static_assert(!std::is_copy_constructible<V>::value, "");
- }
- {
- using V = std::variant<int, MoveOnly>;
- static_assert(!std::is_copy_constructible<V>::value, "");
- }
- {
- using V = std::variant<int, MoveOnlyNT>;
- static_assert(!std::is_copy_constructible<V>::value, "");
- }
- }
- void test_copy_ctor_basic() {
- {
- std::variant<int> v(std::in_place_index<0>, 42);
- std::variant<int> v2 = v;
- assert(v2.index() == 0);
- assert(std::get<0>(v2) == 42);
- }
- {
- std::variant<int, long> v(std::in_place_index<1>, 42);
- std::variant<int, long> v2 = v;
- assert(v2.index() == 1);
- assert(std::get<1>(v2) == 42);
- }
- {
- std::variant<NonT> v(std::in_place_index<0>, 42);
- assert(v.index() == 0);
- std::variant<NonT> v2(v);
- assert(v2.index() == 0);
- assert(std::get<0>(v2).value == 42);
- }
- {
- std::variant<int, NonT> v(std::in_place_index<1>, 42);
- assert(v.index() == 1);
- std::variant<int, NonT> v2(v);
- assert(v2.index() == 1);
- assert(std::get<1>(v2).value == 42);
- }
- }
- void test_copy_ctor_valueless_by_exception() {
- #ifndef TEST_HAS_NO_EXCEPTIONS
- using V = std::variant<int, MakeEmptyT>;
- V v1;
- makeEmpty(v1);
- V const &cv1 = v1;
- V v(cv1);
- assert(v.valueless_by_exception());
- #endif
- }
- int main() {
- test_copy_ctor_basic();
- test_copy_ctor_valueless_by_exception();
- test_copy_ctor_sfinae();
- }
|