| 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 |
// <variant> |
| 12 |
|
| 13 |
// template <class ...Types> class variant; |
| 14 |
|
| 15 |
// constexpr variant() noexcept(see below); |
| 16 |
|
| 17 |
#include <cassert> |
| 18 |
#include <type_traits> |
| 19 |
#include <variant> |
| 20 |
#include <string> |
| 21 |
|
| 22 |
#include "test_macros.h" |
| 23 |
#include "variant_test_helpers.h" |
| 24 |
|
| 25 |
struct NonDefaultConstructible { |
| 26 |
constexpr NonDefaultConstructible(int) {} |
| 27 |
}; |
| 28 |
|
| 29 |
struct NotNoexcept { |
| 30 |
NotNoexcept() noexcept(false) {} |
| 31 |
}; |
| 32 |
|
| 33 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 34 |
struct DefaultCtorThrows { |
| 35 |
DefaultCtorThrows() { throw 42; } |
| 36 |
}; |
| 37 |
#endif |
| 38 |
|
| 39 |
constexpr void test_default_ctor_sfinae() { |
| 40 |
{ |
| 41 |
using V = std::variant<std::monostate, int>; |
| 42 |
static_assert(std::is_default_constructible<V>::value, ""); |
| 43 |
} |
| 44 |
{ |
| 45 |
using V = std::variant<NonDefaultConstructible, int>; |
| 46 |
static_assert(!std::is_default_constructible<V>::value, ""); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
constexpr void test_default_ctor_noexcept() { |
| 51 |
{ |
| 52 |
using V = std::variant<int>; |
| 53 |
static_assert(std::is_nothrow_default_constructible<V>::value, ""); |
| 54 |
} |
| 55 |
{ |
| 56 |
using V = std::variant<NotNoexcept>; |
| 57 |
static_assert(!std::is_nothrow_default_constructible<V>::value, ""); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
void test_default_ctor_throws() { |
| 62 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 63 |
using V = std::variant<DefaultCtorThrows, int>; |
| 64 |
try { |
| 65 |
V v; |
| 66 |
assert(false); |
| 67 |
} catch (const int& ex) { |
| 68 |
assert(ex == 42); |
| 69 |
} catch (...) { |
| 70 |
assert(false); |
| 71 |
} |
| 72 |
#endif |
| 73 |
} |
| 74 |
|
| 75 |
constexpr void test_default_ctor_basic() { |
| 76 |
{ |
| 77 |
std::variant<int> v; |
| 78 |
assert(v.index() == 0); |
| 79 |
assert(std::get<0>(v) == 0); |
| 80 |
} |
| 81 |
{ |
| 82 |
std::variant<int, long> v; |
| 83 |
assert(v.index() == 0); |
| 84 |
assert(std::get<0>(v) == 0); |
| 85 |
} |
| 86 |
{ |
| 87 |
std::variant<int, NonDefaultConstructible> v; |
| 88 |
assert(v.index() == 0); |
| 89 |
assert(std::get<0>(v) == 0); |
| 90 |
} |
| 91 |
{ |
| 92 |
using V = std::variant<int, long>; |
| 93 |
constexpr V v; |
| 94 |
static_assert(v.index() == 0, ""); |
| 95 |
static_assert(std::get<0>(v) == 0, ""); |
| 96 |
} |
| 97 |
{ |
| 98 |
using V = std::variant<int, long>; |
| 99 |
constexpr V v; |
| 100 |
static_assert(v.index() == 0, ""); |
| 101 |
static_assert(std::get<0>(v) == 0, ""); |
| 102 |
} |
| 103 |
{ |
| 104 |
using V = std::variant<int, NonDefaultConstructible>; |
| 105 |
constexpr V v; |
| 106 |
static_assert(v.index() == 0, ""); |
| 107 |
static_assert(std::get<0>(v) == 0, ""); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
constexpr void issue_86686() { |
| 112 |
#if TEST_STD_VER >= 20 |
| 113 |
static_assert(std::variant<std::string>{}.index() == 0); |
| 114 |
#endif |
| 115 |
} |
| 116 |
|
| 117 |
constexpr bool test() { |
| 118 |
test_default_ctor_basic(); |
| 119 |
test_default_ctor_sfinae(); |
| 120 |
test_default_ctor_noexcept(); |
| 121 |
issue_86686(); |
| 122 |
|
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
int main(int, char**) { |
| 127 |
test(); |
| 128 |
test_default_ctor_throws(); |
| 129 |
static_assert(test()); |
| 130 |
return 0; |
| 131 |
} |