2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // UNSUPPORTED: c++98, c++03, c++11, c++14
15 // XFAIL: availability=macosx10.13
16 // XFAIL: availability=macosx10.12
17 // XFAIL: availability=macosx10.11
18 // XFAIL: availability=macosx10.10
19 // XFAIL: availability=macosx10.9
20 // XFAIL: availability=macosx10.8
21 // XFAIL: availability=macosx10.7
23 // template <class ...Types> class variant;
25 // template <class T> constexpr variant(T&&) noexcept(see below);
29 #include <type_traits>
32 #include "test_convertible.hpp"
33 #include "test_macros.h"
34 #include "variant_test_helpers.hpp"
41 ThrowsT(int) noexcept(false) {}
45 NoThrowT(int) noexcept(true) {}
48 struct AnyConstructible { template <typename T> AnyConstructible(T&&) {} };
49 struct NoConstructible { NoConstructible() = delete; };
51 void test_T_ctor_noexcept() {
53 using V = std::variant<Dummy, NoThrowT>;
54 static_assert(std::is_nothrow_constructible<V, int>::value, "");
57 using V = std::variant<Dummy, ThrowsT>;
58 static_assert(!std::is_nothrow_constructible<V, int>::value, "");
62 void test_T_ctor_sfinae() {
64 using V = std::variant<long, unsigned>;
65 static_assert(!std::is_constructible<V, int>::value, "ambiguous");
68 using V = std::variant<std::string, std::string>;
69 static_assert(!std::is_constructible<V, const char *>::value, "ambiguous");
72 using V = std::variant<std::string, void *>;
73 static_assert(!std::is_constructible<V, int>::value,
74 "no matching constructor");
77 using V = std::variant<AnyConstructible, NoConstructible>;
79 !std::is_constructible<V, std::in_place_type_t<NoConstructible>>::value,
80 "no matching constructor");
81 static_assert(!std::is_constructible<V, std::in_place_index_t<1>>::value,
82 "no matching constructor");
87 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
89 using V = std::variant<int, int &&>;
90 static_assert(!std::is_constructible<V, int>::value, "ambiguous");
93 using V = std::variant<int, const int &>;
94 static_assert(!std::is_constructible<V, int>::value, "ambiguous");
99 void test_T_ctor_basic() {
101 constexpr std::variant<int> v(42);
102 static_assert(v.index() == 0, "");
103 static_assert(std::get<0>(v) == 42, "");
106 constexpr std::variant<int, long> v(42l);
107 static_assert(v.index() == 1, "");
108 static_assert(std::get<1>(v) == 42, "");
110 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
112 using V = std::variant<const int &, int &&, long>;
113 static_assert(std::is_convertible<int &, V>::value, "must be implicit");
116 assert(v.index() == 0);
117 assert(&std::get<0>(v) == &x);
120 using V = std::variant<const int &, int &&, long>;
121 static_assert(std::is_convertible<int, V>::value, "must be implicit");
124 assert(v.index() == 1);
125 assert(&std::get<1>(v) == &x);
132 test_T_ctor_noexcept();
133 test_T_ctor_sfinae();