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> T optional<T>::value_or(U&& v) &&; 14 15 #include <optional> 16 #include <type_traits> 17 #include <cassert> 18 19 #include "test_macros.h" 20 21 using std::optional; 22 using std::in_place_t; 23 using std::in_place; 24 25 struct Y 26 { 27 int i_; 28 YY29 Y(int i) : i_(i) {} 30 }; 31 32 struct X 33 { 34 int i_; 35 XX36 X(int i) : i_(i) {} XX37 X(X&& x) : i_(x.i_) {x.i_ = 0;} XX38 X(const Y& y) : i_(y.i_) {} XX39 X(Y&& y) : i_(y.i_+1) {} operator ==(const X & x,const X & y)40 friend constexpr bool operator==(const X& x, const X& y) 41 {return x.i_ == y.i_;} 42 }; 43 main()44int main() 45 { 46 { 47 optional<X> opt(in_place, 2); 48 Y y(3); 49 assert(std::move(opt).value_or(y) == 2); 50 assert(*opt == 0); 51 } 52 { 53 optional<X> opt(in_place, 2); 54 assert(std::move(opt).value_or(Y(3)) == 2); 55 assert(*opt == 0); 56 } 57 { 58 optional<X> opt; 59 Y y(3); 60 assert(std::move(opt).value_or(y) == 3); 61 assert(!opt); 62 } 63 { 64 optional<X> opt; 65 assert(std::move(opt).value_or(Y(3)) == 4); 66 assert(!opt); 67 } 68 } 69