explicit_optional_U.pass.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // explicit optional(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(optional<U>&& rhs, bool is_going_to_throw = false)
  21. {
  22. static_assert(!(std::is_convertible<optional<U>&&, optional<T>>::value), "");
  23. bool rhs_engaged = static_cast<bool>(rhs);
  24. #ifndef TEST_HAS_NO_EXCEPTIONS
  25. try
  26. {
  27. optional<T> lhs(std::move(rhs));
  28. assert(is_going_to_throw == false);
  29. assert(static_cast<bool>(lhs) == rhs_engaged);
  30. }
  31. catch (int i)
  32. {
  33. assert(i == 6);
  34. }
  35. #else
  36. if (is_going_to_throw) return;
  37. optional<T> lhs(std::move(rhs));
  38. assert(static_cast<bool>(lhs) == rhs_engaged);
  39. #endif
  40. }
  41. class X
  42. {
  43. int i_;
  44. public:
  45. explicit X(int i) : i_(i) {}
  46. X(X&& x) : i_(std::exchange(x.i_, 0)) {}
  47. ~X() {i_ = 0;}
  48. friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
  49. };
  50. int count = 0;
  51. class Z
  52. {
  53. public:
  54. explicit Z(int) { TEST_THROW(6); }
  55. };
  56. int main()
  57. {
  58. {
  59. optional<int> rhs;
  60. test<X>(std::move(rhs));
  61. }
  62. {
  63. optional<int> rhs(3);
  64. test<X>(std::move(rhs));
  65. }
  66. {
  67. optional<int> rhs;
  68. test<Z>(std::move(rhs));
  69. }
  70. {
  71. optional<int> rhs(3);
  72. test<Z>(std::move(rhs), true);
  73. }
  74. }