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>::operator*() const &&;
     14 
     15 #ifdef _LIBCPP_DEBUG
     16 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
     17 #endif
     18 
     19 #include <optional>
     20 #include <type_traits>
     21 #include <cassert>
     22 
     23 #include "test_macros.h"
     24 
     25 using std::optional;
     26 
     27 struct X
     28 {
     29     constexpr int test() const& {return 3;}
     30     int test() & {return 4;}
     31     constexpr int test() const&& {return 5;}
     32     int test() && {return 6;}
     33 };
     34 
     35 struct Y
     36 {
     37     int test() const && {return 2;}
     38 };
     39 
     40 int main()
     41 {
     42     {
     43         const optional<X> opt; ((void)opt);
     44         ASSERT_SAME_TYPE(decltype(*std::move(opt)), X const &&);
     45         // ASSERT_NOT_NOEXCEPT(*std::move(opt));
     46         // FIXME: This assertion fails with GCC because it can see that
     47         // (A) operator*() is constexpr, and
     48         // (B) there is no path through the function that throws.
     49         // It's arguable if this is the correct behavior for the noexcept
     50         // operator.
     51         // Regardless this function should still be noexcept(false) because
     52         // it has a narrow contract.
     53     }
     54     {
     55         constexpr optional<X> opt(X{});
     56         static_assert((*std::move(opt)).test() == 5, "");
     57     }
     58     {
     59         constexpr optional<Y> opt(Y{});
     60         assert((*std::move(opt)).test() == 2);
     61     }
     62 #ifdef _LIBCPP_DEBUG
     63     {
     64         optional<X> opt;
     65         assert((*std::move(opt)).test() == 5);
     66         assert(false);
     67     }
     68 #endif  // _LIBCPP_DEBUG
     69 }
     70