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