explicit_optional_U.pass.cpp (1793B)
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(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(optional<U>&& rhs, bool is_going_to_throw = false) 27 { 28 static_assert(!(std::is_convertible<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(std::move(rhs)); 34 assert(is_going_to_throw == false); 35 assert(static_cast<bool>(lhs) == rhs_engaged); 36 } 37 catch (int i) 38 { 39 assert(i == 6); 40 } 41 #else 42 if (is_going_to_throw) return; 43 optional<T> lhs(std::move(rhs)); 44 assert(static_cast<bool>(lhs) == rhs_engaged); 45 #endif 46 } 47 48 class X 49 { 50 int i_; 51 public: 52 explicit X(int i) : i_(i) {} 53 X(X&& x) : i_(std::exchange(x.i_, 0)) {} 54 ~X() {i_ = 0;} 55 friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;} 56 }; 57 58 int count = 0; 59 60 class Z 61 { 62 public: 63 explicit Z(int) { TEST_THROW(6); } 64 }; 65 66 int main() 67 { 68 { 69 optional<int> rhs; 70 test<X>(std::move(rhs)); 71 } 72 { 73 optional<int> rhs(3); 74 test<X>(std::move(rhs)); 75 } 76 { 77 optional<int> rhs; 78 test<Z>(std::move(rhs)); 79 } 80 { 81 optional<int> rhs(3); 82 test<Z>(std::move(rhs), true); 83 } 84 }