blob: 39bf687ff3c5b5ce81f4d541ebbebd1061f21679 [file] [log] [blame]
Howard Hinnant01afa5c2013-09-02 20:30:37 +00001//===----------------------------------------------------------------------===//
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// <optional>
11
12// constexpr const T& optional<T>::value() const;
13
Marshall Clow0cdbe602013-11-15 22:42:10 +000014#include <experimental/optional>
Howard Hinnant01afa5c2013-09-02 20:30:37 +000015#include <type_traits>
16#include <cassert>
17
18#if _LIBCPP_STD_VER > 11
19
Marshall Clow0cdbe602013-11-15 22:42:10 +000020using std::experimental::optional;
21using std::experimental::in_place_t;
22using std::experimental::in_place;
23using std::experimental::bad_optional_access;
24
Howard Hinnant01afa5c2013-09-02 20:30:37 +000025struct X
26{
27 X() = default;
28 X(const X&) = delete;
29 constexpr int test() const {return 3;}
30 int test() {return 4;}
31};
32
33#endif // _LIBCPP_STD_VER > 11
34
35int main()
36{
37#if _LIBCPP_STD_VER > 11
38 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000039 constexpr optional<X> opt(in_place);
Howard Hinnant01afa5c2013-09-02 20:30:37 +000040 static_assert(opt.value().test() == 3, "");
41 }
42 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000043 const optional<X> opt(in_place);
Howard Hinnant01afa5c2013-09-02 20:30:37 +000044 assert(opt.value().test() == 3);
45 }
46 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000047 const optional<X> opt;
Howard Hinnant01afa5c2013-09-02 20:30:37 +000048 try
49 {
50 opt.value();
51 assert(false);
52 }
Marshall Clow0cdbe602013-11-15 22:42:10 +000053 catch (const bad_optional_access&)
Howard Hinnant01afa5c2013-09-02 20:30:37 +000054 {
55 }
56 }
57#endif // _LIBCPP_STD_VER > 11
58}