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) const&;
     14 
     15 #include <optional>
     16 #include <type_traits>
     17 #include <cassert>
     18 
     19 using std::optional;
     20 
     21 struct Y
     22 {
     23     int i_;
     24 
     25     constexpr Y(int i) : i_(i) {}
     26 };
     27 
     28 struct X
     29 {
     30     int i_;
     31 
     32     constexpr X(int i) : i_(i) {}
     33     constexpr X(const Y& y) : i_(y.i_) {}
     34     constexpr X(Y&& y) : i_(y.i_+1) {}
     35     friend constexpr bool operator==(const X& x, const X& y)
     36         {return x.i_ == y.i_;}
     37 };
     38 
     39 int main()
     40 {
     41     {
     42         constexpr optional<X> opt(2);
     43         constexpr Y y(3);
     44         static_assert(opt.value_or(y) == 2, "");
     45     }
     46     {
     47         constexpr optional<X> opt(2);
     48         static_assert(opt.value_or(Y(3)) == 2, "");
     49     }
     50     {
     51         constexpr optional<X> opt;
     52         constexpr Y y(3);
     53         static_assert(opt.value_or(y) == 3, "");
     54     }
     55     {
     56         constexpr optional<X> opt;
     57         static_assert(opt.value_or(Y(3)) == 4, "");
     58     }
     59     {
     60         const optional<X> opt(2);
     61         const Y y(3);
     62         assert(opt.value_or(y) == 2);
     63     }
     64     {
     65         const optional<X> opt(2);
     66         assert(opt.value_or(Y(3)) == 2);
     67     }
     68     {
     69         const optional<X> opt;
     70         const Y y(3);
     71         assert(opt.value_or(y) == 3);
     72     }
     73     {
     74         const optional<X> opt;
     75         assert(opt.value_or(Y(3)) == 4);
     76     }
     77 }
     78