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 // constexpr T& optional<T>::value() &&;
     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::bad_optional_access;
     23 
     24 struct X
     25 {
     26     X() = default;
     27     X(const X&) = delete;
     28     constexpr int test() const & {return 3;}
     29     int test() & {return 4;}
     30     constexpr int test() const && {return 5;}
     31     int test() && {return 6;}
     32 };
     33 
     34 struct Y
     35 {
     36     constexpr int test() && {return 7;}
     37 };
     38 
     39 constexpr int
     40 test()
     41 {
     42     optional<Y> opt{Y{}};
     43     return std::move(opt).value().test();
     44 }
     45 
     46 int main()
     47 {
     48     {
     49         optional<X> opt; ((void)opt);
     50         ASSERT_NOT_NOEXCEPT(std::move(opt).value());
     51         ASSERT_SAME_TYPE(decltype(std::move(opt).value()), X&&);
     52     }
     53     {
     54         optional<X> opt;
     55         opt.emplace();
     56         assert(std::move(opt).value().test() == 6);
     57     }
     58 #ifndef TEST_HAS_NO_EXCEPTIONS
     59     {
     60         optional<X> opt;
     61         try
     62         {
     63             std::move(opt).value();
     64             assert(false);
     65         }
     66         catch (const bad_optional_access&)
     67         {
     68         }
     69     }
     70 #endif
     71     static_assert(test() == 7, "");
     72 }
     73