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