emplace.pass.cpp 3.3 KB

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