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 // UNSUPPORTED: c++98, c++03, c++11, c++14
     11 // <optional>
     12 
     13 // template <class U> constexpr 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 
     29     constexpr Y(int i) : i_(i) {}
     30 };
     31 
     32 struct X
     33 {
     34     int i_;
     35 
     36     constexpr X(int i) : i_(i) {}
     37     constexpr X(X&& x) : i_(x.i_) {x.i_ = 0;}
     38     constexpr X(const Y& y) : i_(y.i_) {}
     39     constexpr X(Y&& y) : i_(y.i_+1) {}
     40     friend constexpr bool operator==(const X& x, const X& y)
     41         {return x.i_ == y.i_;}
     42 };
     43 
     44 constexpr int test()
     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     return 0;
     69 }
     70 
     71 int main()
     72 {
     73     static_assert(test() == 0);
     74 }
     75