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> constexpr T optional<T>::value_or(U&& v) const&;
     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 
     22 struct Y
     23 {
     24     int i_;
     25 
     26     constexpr Y(int i) : i_(i) {}
     27 };
     28 
     29 struct X
     30 {
     31     int i_;
     32 
     33     constexpr X(int i) : i_(i) {}
     34     constexpr X(const Y& y) : i_(y.i_) {}
     35     constexpr X(Y&& y) : i_(y.i_+1) {}
     36     friend constexpr bool operator==(const X& x, const X& y)
     37         {return x.i_ == y.i_;}
     38 };
     39 
     40 #endif  // _LIBCPP_STD_VER > 11
     41 
     42 int main()
     43 {
     44 #if _LIBCPP_STD_VER > 11
     45     {
     46         constexpr optional<X> opt(2);
     47         constexpr Y y(3);
     48         static_assert(opt.value_or(y) == 2, "");
     49     }
     50     {
     51         constexpr optional<X> opt(2);
     52         static_assert(opt.value_or(Y(3)) == 2, "");
     53     }
     54     {
     55         constexpr optional<X> opt;
     56         constexpr Y y(3);
     57         static_assert(opt.value_or(y) == 3, "");
     58     }
     59     {
     60         constexpr optional<X> opt;
     61         static_assert(opt.value_or(Y(3)) == 4, "");
     62     }
     63     {
     64         const optional<X> opt(2);
     65         const Y y(3);
     66         assert(opt.value_or(y) == 2);
     67     }
     68     {
     69         const optional<X> opt(2);
     70         assert(opt.value_or(Y(3)) == 2);
     71     }
     72     {
     73         const optional<X> opt;
     74         const Y y(3);
     75         assert(opt.value_or(y) == 3);
     76     }
     77     {
     78         const optional<X> opt;
     79         assert(opt.value_or(Y(3)) == 4);
     80     }
     81 #endif  // _LIBCPP_STD_VER > 11
     82 }
     83