1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
11 // XFAIL: availability-bad_variant_access-missing && !no-exceptions
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.h"
26 struct NonDefaultConstructible
{
27 constexpr 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 std::variant
<int, NonDefaultConstructible
> v
;
95 assert(v
.index() == 0);
96 assert(std::get
<0>(v
) == 0);
99 using V
= std::variant
<int, long>;
101 static_assert(v
.index() == 0, "");
102 static_assert(std::get
<0>(v
) == 0, "");
105 using V
= std::variant
<int, long>;
107 static_assert(v
.index() == 0, "");
108 static_assert(std::get
<0>(v
) == 0, "");
111 using V
= std::variant
<int, NonDefaultConstructible
>;
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();