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