Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / variant / variant.variant / variant.ctor / default.pass.cpp
blobfd490a3c59ccf4851b42e9b664431ad51651dce8
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
11 // XFAIL: availability-bad_variant_access-missing && !no-exceptions
13 // <variant>
15 // template <class ...Types> class variant;
17 // constexpr variant() noexcept(see below);
19 #include <cassert>
20 #include <type_traits>
21 #include <variant>
23 #include "test_macros.h"
24 #include "variant_test_helpers.h"
26 struct NonDefaultConstructible {
27 constexpr NonDefaultConstructible(int) {}
30 struct NotNoexcept {
31 NotNoexcept() noexcept(false) {}
34 #ifndef TEST_HAS_NO_EXCEPTIONS
35 struct DefaultCtorThrows {
36 DefaultCtorThrows() { throw 42; }
38 #endif
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, "");
54 #endif
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>;
71 try {
72 V v;
73 assert(false);
74 } catch (const int &ex) {
75 assert(ex == 42);
76 } catch (...) {
77 assert(false);
79 #endif
82 void test_default_ctor_basic() {
84 std::variant<int> v;
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 std::variant<int, NonDefaultConstructible> v;
95 assert(v.index() == 0);
96 assert(std::get<0>(v) == 0);
99 using V = std::variant<int, long>;
100 constexpr V v;
101 static_assert(v.index() == 0, "");
102 static_assert(std::get<0>(v) == 0, "");
105 using V = std::variant<int, long>;
106 constexpr V v;
107 static_assert(v.index() == 0, "");
108 static_assert(std::get<0>(v) == 0, "");
111 using V = std::variant<int, NonDefaultConstructible>;
112 constexpr V v;
113 static_assert(v.index() == 0, "");
114 static_assert(std::get<0>(v) == 0, "");
118 int main(int, char**) {
119 test_default_ctor_basic();
120 test_default_ctor_sfinae();
121 test_default_ctor_noexcept();
122 test_default_ctor_throws();
124 return 0;