Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 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 | // <optional> |
| 11 | |
| 12 | // T& optional<T>::value(); |
| 13 | |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 14 | #include <experimental/optional> |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 15 | #include <type_traits> |
| 16 | #include <cassert> |
| 17 | |
| 18 | #if _LIBCPP_STD_VER > 11 |
| 19 | |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 20 | using std::experimental::optional; |
| 21 | using std::experimental::bad_optional_access; |
| 22 | |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 23 | struct X |
| 24 | { |
| 25 | X() = default; |
| 26 | X(const X&) = delete; |
| 27 | constexpr int test() const {return 3;} |
| 28 | int test() {return 4;} |
| 29 | }; |
| 30 | |
| 31 | #endif // _LIBCPP_STD_VER > 11 |
| 32 | |
| 33 | int main() |
| 34 | { |
| 35 | #if _LIBCPP_STD_VER > 11 |
| 36 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 37 | optional<X> opt; |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 38 | opt.emplace(); |
| 39 | assert(opt.value().test() == 4); |
| 40 | } |
| 41 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 42 | optional<X> opt; |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 43 | try |
| 44 | { |
| 45 | opt.value(); |
| 46 | assert(false); |
| 47 | } |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 48 | catch (const bad_optional_access&) |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | } |
| 52 | #endif // _LIBCPP_STD_VER > 11 |
| 53 | } |