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 
     12 // XFAIL: availability=macosx10.13
     13 // XFAIL: availability=macosx10.12
     14 // XFAIL: availability=macosx10.11
     15 // XFAIL: availability=macosx10.10
     16 // XFAIL: availability=macosx10.9
     17 // XFAIL: availability=macosx10.8
     18 // XFAIL: availability=macosx10.7
     19 
     20 // <optional>
     21 
     22 // constexpr T& optional<T>::value() &;
     23 
     24 #include <optional>
     25 #include <type_traits>
     26 #include <cassert>
     27 
     28 #include "test_macros.h"
     29 
     30 using std::optional;
     31 using std::bad_optional_access;
     32 
     33 struct X
     34 {
     35     X() = default;
     36     X(const X&) = delete;
     37     constexpr int test() const & {return 3;}
     38     int test() & {return 4;}
     39     constexpr int test() const && {return 5;}
     40     int test() && {return 6;}
     41 };
     42 
     43 struct Y
     44 {
     45     constexpr int test() & {return 7;}
     46 };
     47 
     48 constexpr int
     49 test()
     50 {
     51     optional<Y> opt{Y{}};
     52     return opt.value().test();
     53 }
     54 
     55 
     56 int main()
     57 {
     58     {
     59         optional<X> opt; ((void)opt);
     60         ASSERT_NOT_NOEXCEPT(opt.value());
     61         ASSERT_SAME_TYPE(decltype(opt.value()), X&);
     62     }
     63     {
     64         optional<X> opt;
     65         opt.emplace();
     66         assert(opt.value().test() == 4);
     67     }
     68 #ifndef TEST_HAS_NO_EXCEPTIONS
     69     {
     70         optional<X> opt;
     71         try
     72         {
     73             (void)opt.value();
     74             assert(false);
     75         }
     76         catch (const bad_optional_access&)
     77         {
     78         }
     79     }
     80 #endif
     81     static_assert(test() == 7, "");
     82 }
     83