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 // constexpr variant() noexcept(see below);
20 #include <type_traits>
23 #include "test_macros.h"
24 #include "variant_test_helpers.hpp"
26 struct NonDefaultConstructible {
27 NonDefaultConstructible(int) {}
31 NotNoexcept() noexcept(false) {}
34 #ifndef TEST_HAS_NO_EXCEPTIONS
35 struct DefaultCtorThrows {
36 DefaultCtorThrows() { throw 42; }
40 void test_default_ctor_sfinae() {
42 using V = std::variant<std::monostate, int>;
43 static_assert(std::is_default_constructible<V>::value, "");
46 using V = std::variant<NonDefaultConstructible, int>;
47 static_assert(!std::is_default_constructible<V>::value, "");
49 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
51 using V = std::variant<int &, int>;
52 static_assert(!std::is_default_constructible<V>::value, "");
57 void test_default_ctor_noexcept() {
59 using V = std::variant<int>;
60 static_assert(std::is_nothrow_default_constructible<V>::value, "");
63 using V = std::variant<NotNoexcept>;
64 static_assert(!std::is_nothrow_default_constructible<V>::value, "");
68 void test_default_ctor_throws() {
69 #ifndef TEST_HAS_NO_EXCEPTIONS
70 using V = std::variant<DefaultCtorThrows, int>;
74 } catch (const int &ex) {
82 void test_default_ctor_basic() {
85 assert(v.index() == 0);
86 assert(std::get<0>(v) == 0);
89 std::variant<int, long> v;
90 assert(v.index() == 0);
91 assert(std::get<0>(v) == 0);
94 using V = std::variant<int, long>;
96 static_assert(v.index() == 0, "");
97 static_assert(std::get<0>(v) == 0, "");
100 using V = std::variant<int, long>;
102 static_assert(v.index() == 0, "");
103 static_assert(std::get<0>(v) == 0, "");
108 test_default_ctor_basic();
109 test_default_ctor_sfinae();
110 test_default_ctor_noexcept();
111 test_default_ctor_throws();