1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // Throwing bad_variant_access is supported starting in macosx10.13
12 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
13 
14 // <variant>
15 
16 // template <class ...Types> class variant;
17 
18 // constexpr variant() noexcept(see below);
19 
20 #include <cassert>
21 #include <type_traits>
22 #include <variant>
23 
24 #include "test_macros.h"
25 #include "variant_test_helpers.h"
26 
27 struct NonDefaultConstructible {
NonDefaultConstructibleNonDefaultConstructible28   constexpr NonDefaultConstructible(int) {}
29 };
30 
31 struct NotNoexcept {
NotNoexceptNotNoexcept32   NotNoexcept() noexcept(false) {}
33 };
34 
35 #ifndef TEST_HAS_NO_EXCEPTIONS
36 struct DefaultCtorThrows {
DefaultCtorThrowsDefaultCtorThrows37   DefaultCtorThrows() { throw 42; }
38 };
39 #endif
40 
test_default_ctor_sfinae()41 void test_default_ctor_sfinae() {
42   {
43     using V = std::variant<std::monostate, int>;
44     static_assert(std::is_default_constructible<V>::value, "");
45   }
46   {
47     using V = std::variant<NonDefaultConstructible, int>;
48     static_assert(!std::is_default_constructible<V>::value, "");
49   }
50 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
51   {
52     using V = std::variant<int &, int>;
53     static_assert(!std::is_default_constructible<V>::value, "");
54   }
55 #endif
56 }
57 
test_default_ctor_noexcept()58 void test_default_ctor_noexcept() {
59   {
60     using V = std::variant<int>;
61     static_assert(std::is_nothrow_default_constructible<V>::value, "");
62   }
63   {
64     using V = std::variant<NotNoexcept>;
65     static_assert(!std::is_nothrow_default_constructible<V>::value, "");
66   }
67 }
68 
test_default_ctor_throws()69 void test_default_ctor_throws() {
70 #ifndef TEST_HAS_NO_EXCEPTIONS
71   using V = std::variant<DefaultCtorThrows, int>;
72   try {
73     V v;
74     assert(false);
75   } catch (const int &ex) {
76     assert(ex == 42);
77   } catch (...) {
78     assert(false);
79   }
80 #endif
81 }
82 
test_default_ctor_basic()83 void test_default_ctor_basic() {
84   {
85     std::variant<int> v;
86     assert(v.index() == 0);
87     assert(std::get<0>(v) == 0);
88   }
89   {
90     std::variant<int, long> v;
91     assert(v.index() == 0);
92     assert(std::get<0>(v) == 0);
93   }
94   {
95     std::variant<int, NonDefaultConstructible> v;
96     assert(v.index() == 0);
97     assert(std::get<0>(v) == 0);
98   }
99   {
100     using V = std::variant<int, long>;
101     constexpr V v;
102     static_assert(v.index() == 0, "");
103     static_assert(std::get<0>(v) == 0, "");
104   }
105   {
106     using V = std::variant<int, long>;
107     constexpr V v;
108     static_assert(v.index() == 0, "");
109     static_assert(std::get<0>(v) == 0, "");
110   }
111   {
112     using V = std::variant<int, NonDefaultConstructible>;
113     constexpr V v;
114     static_assert(v.index() == 0, "");
115     static_assert(std::get<0>(v) == 0, "");
116   }
117 }
118 
main(int,char **)119 int main(int, char**) {
120   test_default_ctor_basic();
121   test_default_ctor_sfinae();
122   test_default_ctor_noexcept();
123   test_default_ctor_throws();
124 
125   return 0;
126 }
127