const_optional_U.pass.cpp 2.6 KB

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