in_place_t.pass.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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++98, c++03, c++11, c++14
  10. // <optional>
  11. // template <class... Args>
  12. // constexpr explicit optional(in_place_t, Args&&... args);
  13. #include <optional>
  14. #include <type_traits>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. using std::optional;
  18. using std::in_place_t;
  19. using std::in_place;
  20. class X
  21. {
  22. int i_;
  23. int j_ = 0;
  24. public:
  25. X() : i_(0) {}
  26. X(int i) : i_(i) {}
  27. X(int i, int j) : i_(i), j_(j) {}
  28. ~X() {}
  29. friend bool operator==(const X& x, const X& y)
  30. {return x.i_ == y.i_ && x.j_ == y.j_;}
  31. };
  32. class Y
  33. {
  34. int i_;
  35. int j_ = 0;
  36. public:
  37. constexpr Y() : i_(0) {}
  38. constexpr Y(int i) : i_(i) {}
  39. constexpr Y(int i, int j) : i_(i), j_(j) {}
  40. friend constexpr bool operator==(const Y& x, const Y& y)
  41. {return x.i_ == y.i_ && x.j_ == y.j_;}
  42. };
  43. class Z
  44. {
  45. public:
  46. Z(int) {TEST_THROW(6);}
  47. };
  48. int main(int, char**)
  49. {
  50. {
  51. constexpr optional<int> opt(in_place, 5);
  52. static_assert(static_cast<bool>(opt) == true, "");
  53. static_assert(*opt == 5, "");
  54. struct test_constexpr_ctor
  55. : public optional<int>
  56. {
  57. constexpr test_constexpr_ctor(in_place_t, int i)
  58. : optional<int>(in_place, i) {}
  59. };
  60. }
  61. {
  62. optional<const int> opt(in_place, 5);
  63. assert(*opt == 5);
  64. }
  65. {
  66. const optional<X> opt(in_place);
  67. assert(static_cast<bool>(opt) == true);
  68. assert(*opt == X());
  69. }
  70. {
  71. const optional<X> opt(in_place, 5);
  72. assert(static_cast<bool>(opt) == true);
  73. assert(*opt == X(5));
  74. }
  75. {
  76. const optional<X> opt(in_place, 5, 4);
  77. assert(static_cast<bool>(opt) == true);
  78. assert(*opt == X(5, 4));
  79. }
  80. {
  81. constexpr optional<Y> opt(in_place);
  82. static_assert(static_cast<bool>(opt) == true, "");
  83. static_assert(*opt == Y(), "");
  84. struct test_constexpr_ctor
  85. : public optional<Y>
  86. {
  87. constexpr test_constexpr_ctor(in_place_t)
  88. : optional<Y>(in_place) {}
  89. };
  90. }
  91. {
  92. constexpr optional<Y> opt(in_place, 5);
  93. static_assert(static_cast<bool>(opt) == true, "");
  94. static_assert(*opt == Y(5), "");
  95. struct test_constexpr_ctor
  96. : public optional<Y>
  97. {
  98. constexpr test_constexpr_ctor(in_place_t, int i)
  99. : optional<Y>(in_place, i) {}
  100. };
  101. }
  102. {
  103. constexpr optional<Y> opt(in_place, 5, 4);
  104. static_assert(static_cast<bool>(opt) == true, "");
  105. static_assert(*opt == Y(5, 4), "");
  106. struct test_constexpr_ctor
  107. : public optional<Y>
  108. {
  109. constexpr test_constexpr_ctor(in_place_t, int i, int j)
  110. : optional<Y>(in_place, i, j) {}
  111. };
  112. }
  113. #ifndef TEST_HAS_NO_EXCEPTIONS
  114. {
  115. try
  116. {
  117. const optional<Z> opt(in_place, 1);
  118. assert(false);
  119. }
  120. catch (int i)
  121. {
  122. assert(i == 6);
  123. }
  124. }
  125. #endif
  126. return 0;
  127. }