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 // template <class ...Types> class variant;
17 // template <class T> constexpr variant(T&&) noexcept(see below);
21 #include <type_traits>
24 #include "test_convertible.hpp"
25 #include "test_macros.h"
26 #include "variant_test_helpers.hpp"
33 ThrowsT(int) noexcept(false) {}
37 NoThrowT(int) noexcept(true) {}
40 void test_T_ctor_noexcept() {
42 using V = std::variant<Dummy, NoThrowT>;
43 static_assert(std::is_nothrow_constructible<V, int>::value, "");
46 using V = std::variant<Dummy, ThrowsT>;
47 static_assert(!std::is_nothrow_constructible<V, int>::value, "");
51 void test_T_ctor_sfinae() {
53 using V = std::variant<long, unsigned>;
54 static_assert(!std::is_constructible<V, int>::value, "ambiguous");
57 using V = std::variant<std::string, std::string>;
58 static_assert(!std::is_constructible<V, const char *>::value, "ambiguous");
61 using V = std::variant<std::string, void *>;
62 static_assert(!std::is_constructible<V, int>::value,
63 "no matching constructor");
65 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
67 using V = std::variant<int, int &&>;
68 static_assert(!std::is_constructible<V, int>::value, "ambiguous");
71 using V = std::variant<int, const int &>;
72 static_assert(!std::is_constructible<V, int>::value, "ambiguous");
77 void test_T_ctor_basic() {
79 constexpr std::variant<int> v(42);
80 static_assert(v.index() == 0, "");
81 static_assert(std::get<0>(v) == 42, "");
84 constexpr std::variant<int, long> v(42l);
85 static_assert(v.index() == 1, "");
86 static_assert(std::get<1>(v) == 42, "");
88 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
90 using V = std::variant<const int &, int &&, long>;
91 static_assert(std::is_convertible<int &, V>::value, "must be implicit");
94 assert(v.index() == 0);
95 assert(&std::get<0>(v) == &x);
98 using V = std::variant<const int &, int &&, long>;
99 static_assert(std::is_convertible<int, V>::value, "must be implicit");
102 assert(v.index() == 1);
103 assert(&std::get<1>(v) == &x);
110 test_T_ctor_noexcept();
111 test_T_ctor_sfinae();