Home | History | Annotate | Download | only in optional.object.observe
      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 U> T optional<T>::value_or(U&& v) &&;
     13 
     14 #include <experimental/optional>
     15 #include <type_traits>
     16 #include <cassert>
     17 
     18 #if _LIBCPP_STD_VER > 11
     19 
     20 using std::experimental::optional;
     21 using std::experimental::in_place_t;
     22 using std::experimental::in_place;
     23 
     24 struct Y
     25 {
     26     int i_;
     27 
     28     Y(int i) : i_(i) {}
     29 };
     30 
     31 struct X
     32 {
     33     int i_;
     34 
     35     X(int i) : i_(i) {}
     36     X(X&& x) : i_(x.i_) {x.i_ = 0;}
     37     X(const Y& y) : i_(y.i_) {}
     38     X(Y&& y) : i_(y.i_+1) {}
     39     friend constexpr bool operator==(const X& x, const X& y)
     40         {return x.i_ == y.i_;}
     41 };
     42 
     43 #endif  // _LIBCPP_STD_VER > 11
     44 
     45 int main()
     46 {
     47 #if _LIBCPP_STD_VER > 11
     48     {
     49         optional<X> opt(in_place, 2);
     50         Y y(3);
     51         assert(std::move(opt).value_or(y) == 2);
     52         assert(*opt == 0);
     53     }
     54     {
     55         optional<X> opt(in_place, 2);
     56         assert(std::move(opt).value_or(Y(3)) == 2);
     57         assert(*opt == 0);
     58     }
     59     {
     60         optional<X> opt;
     61         Y y(3);
     62         assert(std::move(opt).value_or(y) == 3);
     63         assert(!opt);
     64     }
     65     {
     66         optional<X> opt;
     67         assert(std::move(opt).value_or(Y(3)) == 4);
     68         assert(!opt);
     69     }
     70 #endif  // _LIBCPP_STD_VER > 11
     71 }
     72