1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 // UNSUPPORTED: c++98, c++03, c++11, c++14
12
13 // XFAIL: availability=macosx10.13
14 // XFAIL: availability=macosx10.12
15 // XFAIL: availability=macosx10.11
16 // XFAIL: availability=macosx10.10
17 // XFAIL: availability=macosx10.9
18 // XFAIL: availability=macosx10.8
19 // XFAIL: availability=macosx10.7
20
21 // <variant>
22
23 // template <class ...Types> class variant;
24
25 // constexpr variant() noexcept(see below);
26
27 #include <cassert>
28 #include <type_traits>
29 #include <variant>
30
31 #include "test_macros.h"
32 #include "variant_test_helpers.hpp"
33
34 struct NonDefaultConstructible {
NonDefaultConstructibleNonDefaultConstructible35 constexpr NonDefaultConstructible(int) {}
36 };
37
38 struct NotNoexcept {
NotNoexceptNotNoexcept39 NotNoexcept() noexcept(false) {}
40 };
41
42 #ifndef TEST_HAS_NO_EXCEPTIONS
43 struct DefaultCtorThrows {
DefaultCtorThrowsDefaultCtorThrows44 DefaultCtorThrows() { throw 42; }
45 };
46 #endif
47
test_default_ctor_sfinae()48 void test_default_ctor_sfinae() {
49 {
50 using V = std::variant<std::monostate, int>;
51 static_assert(std::is_default_constructible<V>::value, "");
52 }
53 {
54 using V = std::variant<NonDefaultConstructible, int>;
55 static_assert(!std::is_default_constructible<V>::value, "");
56 }
57 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
58 {
59 using V = std::variant<int &, int>;
60 static_assert(!std::is_default_constructible<V>::value, "");
61 }
62 #endif
63 }
64
test_default_ctor_noexcept()65 void test_default_ctor_noexcept() {
66 {
67 using V = std::variant<int>;
68 static_assert(std::is_nothrow_default_constructible<V>::value, "");
69 }
70 {
71 using V = std::variant<NotNoexcept>;
72 static_assert(!std::is_nothrow_default_constructible<V>::value, "");
73 }
74 }
75
test_default_ctor_throws()76 void test_default_ctor_throws() {
77 #ifndef TEST_HAS_NO_EXCEPTIONS
78 using V = std::variant<DefaultCtorThrows, int>;
79 try {
80 V v;
81 assert(false);
82 } catch (const int &ex) {
83 assert(ex == 42);
84 } catch (...) {
85 assert(false);
86 }
87 #endif
88 }
89
test_default_ctor_basic()90 void test_default_ctor_basic() {
91 {
92 std::variant<int> v;
93 assert(v.index() == 0);
94 assert(std::get<0>(v) == 0);
95 }
96 {
97 std::variant<int, long> v;
98 assert(v.index() == 0);
99 assert(std::get<0>(v) == 0);
100 }
101 {
102 std::variant<int, NonDefaultConstructible> v;
103 assert(v.index() == 0);
104 assert(std::get<0>(v) == 0);
105 }
106 {
107 using V = std::variant<int, long>;
108 constexpr V v;
109 static_assert(v.index() == 0, "");
110 static_assert(std::get<0>(v) == 0, "");
111 }
112 {
113 using V = std::variant<int, long>;
114 constexpr V v;
115 static_assert(v.index() == 0, "");
116 static_assert(std::get<0>(v) == 0, "");
117 }
118 {
119 using V = std::variant<int, NonDefaultConstructible>;
120 constexpr V v;
121 static_assert(v.index() == 0, "");
122 static_assert(std::get<0>(v) == 0, "");
123 }
124 }
125
main()126 int main() {
127 test_default_ctor_basic();
128 test_default_ctor_sfinae();
129 test_default_ctor_noexcept();
130 test_default_ctor_throws();
131 }
132