default.pass.cpp 2.5 KB

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