]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/utilities/variant/variant.variant/variant.ctor/default.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / utilities / variant / variant.variant / variant.ctor / default.pass.cpp
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 // <variant>
14
15 // template <class ...Types> class variant;
16
17 // constexpr variant() noexcept(see below);
18
19 #include <cassert>
20 #include <type_traits>
21 #include <variant>
22
23 #include "test_macros.h"
24 #include "variant_test_helpers.hpp"
25
26 struct NonDefaultConstructible {
27   NonDefaultConstructible(int) {}
28 };
29
30 struct NotNoexcept {
31   NotNoexcept() noexcept(false) {}
32 };
33
34 #ifndef TEST_HAS_NO_EXCEPTIONS
35 struct DefaultCtorThrows {
36   DefaultCtorThrows() { throw 42; }
37 };
38 #endif
39
40 void test_default_ctor_sfinae() {
41   {
42     using V = std::variant<std::monostate, int>;
43     static_assert(std::is_default_constructible<V>::value, "");
44   }
45   {
46     using V = std::variant<NonDefaultConstructible, int>;
47     static_assert(!std::is_default_constructible<V>::value, "");
48   }
49 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
50   {
51     using V = std::variant<int &, int>;
52     static_assert(!std::is_default_constructible<V>::value, "");
53   }
54 #endif
55 }
56
57 void test_default_ctor_noexcept() {
58   {
59     using V = std::variant<int>;
60     static_assert(std::is_nothrow_default_constructible<V>::value, "");
61   }
62   {
63     using V = std::variant<NotNoexcept>;
64     static_assert(!std::is_nothrow_default_constructible<V>::value, "");
65   }
66 }
67
68 void test_default_ctor_throws() {
69 #ifndef TEST_HAS_NO_EXCEPTIONS
70   using V = std::variant<DefaultCtorThrows, int>;
71   try {
72     V v;
73     assert(false);
74   } catch (const int &ex) {
75     assert(ex == 42);
76   } catch (...) {
77     assert(false);
78   }
79 #endif
80 }
81
82 void test_default_ctor_basic() {
83   {
84     std::variant<int> v;
85     assert(v.index() == 0);
86     assert(std::get<0>(v) == 0);
87   }
88   {
89     std::variant<int, long> v;
90     assert(v.index() == 0);
91     assert(std::get<0>(v) == 0);
92   }
93   {
94     using V = std::variant<int, long>;
95     constexpr V v;
96     static_assert(v.index() == 0, "");
97     static_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
107 int main() {
108   test_default_ctor_basic();
109   test_default_ctor_sfinae();
110   test_default_ctor_noexcept();
111   test_default_ctor_throws();
112 }