const_T.pass.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // XFAIL: dylib-has-no-bad_optional_access && !libcpp-no-exceptions
  11. // <optional>
  12. // constexpr optional(const T& v);
  13. #include <optional>
  14. #include <type_traits>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. #include "archetypes.h"
  18. using std::optional;
  19. int main(int, char**)
  20. {
  21. {
  22. typedef int T;
  23. constexpr T t(5);
  24. constexpr optional<T> opt(t);
  25. static_assert(static_cast<bool>(opt) == true, "");
  26. static_assert(*opt == 5, "");
  27. struct test_constexpr_ctor
  28. : public optional<T>
  29. {
  30. constexpr test_constexpr_ctor(const T&) {}
  31. };
  32. }
  33. {
  34. typedef double T;
  35. constexpr T t(3);
  36. constexpr optional<T> opt(t);
  37. static_assert(static_cast<bool>(opt) == true, "");
  38. static_assert(*opt == 3, "");
  39. struct test_constexpr_ctor
  40. : public optional<T>
  41. {
  42. constexpr test_constexpr_ctor(const T&) {}
  43. };
  44. }
  45. {
  46. const int x = 42;
  47. optional<const int> o(x);
  48. assert(*o == x);
  49. }
  50. {
  51. typedef TestTypes::TestType T;
  52. T::reset();
  53. const T t(3);
  54. optional<T> opt = t;
  55. assert(T::alive == 2);
  56. assert(T::copy_constructed == 1);
  57. assert(static_cast<bool>(opt) == true);
  58. assert(opt.value().value == 3);
  59. }
  60. {
  61. typedef ExplicitTestTypes::TestType T;
  62. static_assert(!std::is_convertible<T const&, optional<T>>::value, "");
  63. T::reset();
  64. const T t(3);
  65. optional<T> opt(t);
  66. assert(T::alive == 2);
  67. assert(T::copy_constructed == 1);
  68. assert(static_cast<bool>(opt) == true);
  69. assert(opt.value().value == 3);
  70. }
  71. {
  72. typedef ConstexprTestTypes::TestType T;
  73. constexpr T t(3);
  74. constexpr optional<T> opt = {t};
  75. static_assert(static_cast<bool>(opt) == true, "");
  76. static_assert(opt.value().value == 3, "");
  77. struct test_constexpr_ctor
  78. : public optional<T>
  79. {
  80. constexpr test_constexpr_ctor(const T&) {}
  81. };
  82. }
  83. {
  84. typedef ExplicitConstexprTestTypes::TestType T;
  85. static_assert(!std::is_convertible<const T&, optional<T>>::value, "");
  86. constexpr T t(3);
  87. constexpr optional<T> opt(t);
  88. static_assert(static_cast<bool>(opt) == true, "");
  89. static_assert(opt.value().value == 3, "");
  90. struct test_constexpr_ctor
  91. : public optional<T>
  92. {
  93. constexpr test_constexpr_ctor(const T&) {}
  94. };
  95. }
  96. #ifndef TEST_HAS_NO_EXCEPTIONS
  97. {
  98. struct Z {
  99. Z(int) {}
  100. Z(const Z&) {throw 6;}
  101. };
  102. typedef Z T;
  103. try
  104. {
  105. const T t(3);
  106. optional<T> opt(t);
  107. assert(false);
  108. }
  109. catch (int i)
  110. {
  111. assert(i == 6);
  112. }
  113. }
  114. #endif
  115. return 0;
  116. }