explicit_const_optional_U.pass.cpp (2481B)
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, c++14 11 // <optional> 12 13 // template <class U> 14 // explicit optional(const optional<U>& rhs); 15 16 #include <optional> 17 #include <type_traits> 18 #include <cassert> 19 20 #include "test_macros.h" 21 22 using std::optional; 23 24 template <class T, class U> 25 void 26 test(const optional<U>& rhs, bool is_going_to_throw = false) 27 { 28 static_assert(!(std::is_convertible<const optional<U>&, optional<T>>::value), ""); 29 bool rhs_engaged = static_cast<bool>(rhs); 30 #ifndef TEST_HAS_NO_EXCEPTIONS 31 try 32 { 33 optional<T> lhs(rhs); 34 assert(is_going_to_throw == false); 35 assert(static_cast<bool>(lhs) == rhs_engaged); 36 if (rhs_engaged) 37 assert(*lhs == T(*rhs)); 38 } 39 catch (int i) 40 { 41 assert(i == 6); 42 } 43 #else 44 if (is_going_to_throw) return; 45 optional<T> lhs(rhs); 46 assert(static_cast<bool>(lhs) == rhs_engaged); 47 if (rhs_engaged) 48 assert(*lhs == T(*rhs)); 49 #endif 50 } 51 52 class X 53 { 54 int i_; 55 public: 56 explicit X(int i) : i_(i) {} 57 X(const X& x) : i_(x.i_) {} 58 ~X() {i_ = 0;} 59 friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;} 60 }; 61 62 class Y 63 { 64 int i_; 65 public: 66 explicit Y(int i) : i_(i) {} 67 68 friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;} 69 }; 70 71 int count = 0; 72 73 class Z 74 { 75 int i_; 76 public: 77 explicit Z(int i) : i_(i) { TEST_THROW(6);} 78 79 friend constexpr bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;} 80 }; 81 82 83 int main() 84 { 85 { 86 typedef X T; 87 typedef int U; 88 optional<U> rhs; 89 test<T>(rhs); 90 } 91 { 92 typedef X T; 93 typedef int U; 94 optional<U> rhs(3); 95 test<T>(rhs); 96 } 97 { 98 typedef Y T; 99 typedef int U; 100 optional<U> rhs; 101 test<T>(rhs); 102 } 103 { 104 typedef Y T; 105 typedef int U; 106 optional<U> rhs(3); 107 test<T>(rhs); 108 } 109 { 110 typedef Z T; 111 typedef int U; 112 optional<U> rhs; 113 test<T>(rhs); 114 } 115 { 116 typedef Z T; 117 typedef int U; 118 optional<U> rhs(3); 119 test<T>(rhs, true); 120 } 121 }