copy.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// optional<T>& operator=(const optional<T>& rhs); 13 14#include <optional> 15#include <type_traits> 16#include <cassert> 17 18#if _LIBCPP_STD_VER > 11 19 20struct X 21{ 22 static bool throw_now; 23 24 X() = default; 25 X(const X&) 26 { 27 if (throw_now) 28 throw 6; 29 } 30}; 31 32bool X::throw_now = false; 33 34#endif // _LIBCPP_STD_VER > 11 35 36int main() 37{ 38#if _LIBCPP_STD_VER > 11 39 { 40 std::optional<int> opt; 41 constexpr std::optional<int> opt2; 42 opt = opt2; 43 static_assert(static_cast<bool>(opt2) == false, ""); 44 assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); 45 } 46 { 47 std::optional<int> opt; 48 constexpr std::optional<int> opt2(2); 49 opt = opt2; 50 static_assert(static_cast<bool>(opt2) == true, ""); 51 static_assert(*opt2 == 2, ""); 52 assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); 53 assert(*opt == *opt2); 54 } 55 { 56 std::optional<int> opt(3); 57 constexpr std::optional<int> opt2; 58 opt = opt2; 59 static_assert(static_cast<bool>(opt2) == false, ""); 60 assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); 61 } 62 { 63 std::optional<int> opt(3); 64 constexpr std::optional<int> opt2(2); 65 opt = opt2; 66 static_assert(static_cast<bool>(opt2) == true, ""); 67 static_assert(*opt2 == 2, ""); 68 assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); 69 assert(*opt == *opt2); 70 } 71 { 72 std::optional<X> opt; 73 std::optional<X> opt2(X{}); 74 assert(static_cast<bool>(opt2) == true); 75 try 76 { 77 X::throw_now = true; 78 opt = opt2; 79 assert(false); 80 } 81 catch (int i) 82 { 83 assert(i == 6); 84 assert(static_cast<bool>(opt) == false); 85 } 86 } 87#endif // _LIBCPP_STD_VER > 11 88} 89