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