const_optional_U.pass.cpp 2.6 KB

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