|
6 | 6 | // |
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
| 9 | +// REQUIRES: std-at-least-c++17 |
| 10 | + |
9 | 11 | // <optional> |
10 | | -// UNSUPPORTED: c++03, c++11, c++14 |
11 | 12 |
|
12 | 13 | // template<class T> |
13 | 14 | // optional(T) -> optional<T>; |
14 | 15 |
|
15 | | -#include <optional> |
16 | 16 | #include <cassert> |
| 17 | +#include <optional> |
17 | 18 |
|
18 | 19 | #include "test_macros.h" |
19 | 20 |
|
20 | | -struct A {}; |
| 21 | +struct A { |
| 22 | + friend constexpr bool operator==(const A&, const A&) { return true; } |
| 23 | +}; |
21 | 24 |
|
22 | | -int main(int, char**) |
23 | | -{ |
24 | | -// Test the explicit deduction guides |
25 | | - { |
26 | | -// optional(T) |
27 | | - std::optional opt(5); |
28 | | - ASSERT_SAME_TYPE(decltype(opt), std::optional<int>); |
29 | | - assert(static_cast<bool>(opt)); |
30 | | - assert(*opt == 5); |
31 | | - } |
| 25 | +template <typename T> |
| 26 | +constexpr void test_deduct(T arg) { |
| 27 | + std::optional opt(arg); |
32 | 28 |
|
33 | | - { |
34 | | -// optional(T) |
35 | | - std::optional opt(A{}); |
36 | | - ASSERT_SAME_TYPE(decltype(opt), std::optional<A>); |
37 | | - assert(static_cast<bool>(opt)); |
38 | | - } |
| 29 | + ASSERT_SAME_TYPE(decltype(opt), std::optional<T>); |
| 30 | + assert(static_cast<bool>(opt)); |
| 31 | + assert(*opt == arg); |
| 32 | +} |
39 | 33 |
|
40 | | - { |
41 | | -// optional(const T&); |
| 34 | +constexpr bool test() { |
| 35 | + // optional(T) |
| 36 | + test_deduct<int>(5); |
| 37 | + test_deduct<A>(A{}); |
| 38 | + |
| 39 | + { |
| 40 | + // optional(const T&); |
42 | 41 | const int& source = 5; |
43 | | - std::optional opt(source); |
44 | | - ASSERT_SAME_TYPE(decltype(opt), std::optional<int>); |
45 | | - assert(static_cast<bool>(opt)); |
46 | | - assert(*opt == 5); |
47 | | - } |
| 42 | + test_deduct<int>(source); |
| 43 | + } |
48 | 44 |
|
49 | | - { |
50 | | -// optional(T*); |
| 45 | + { |
| 46 | + // optional(T*); |
51 | 47 | const int* source = nullptr; |
52 | | - std::optional opt(source); |
53 | | - ASSERT_SAME_TYPE(decltype(opt), std::optional<const int*>); |
54 | | - assert(static_cast<bool>(opt)); |
55 | | - assert(*opt == nullptr); |
56 | | - } |
| 48 | + test_deduct<const int*>(source); |
| 49 | + } |
57 | 50 |
|
58 | | - { |
59 | | -// optional(T[]); |
| 51 | + { |
| 52 | + // optional(T[]); |
60 | 53 | int source[] = {1, 2, 3}; |
61 | 54 | std::optional opt(source); |
| 55 | + |
62 | 56 | ASSERT_SAME_TYPE(decltype(opt), std::optional<int*>); |
63 | 57 | assert(static_cast<bool>(opt)); |
64 | 58 | assert((*opt)[0] == 1); |
65 | | - } |
| 59 | + } |
66 | 60 |
|
67 | | -// Test the implicit deduction guides |
68 | | - { |
69 | | -// optional(optional); |
| 61 | + // Test the implicit deduction guides |
| 62 | + { |
| 63 | + // optional(optional); |
70 | 64 | std::optional<char> source('A'); |
71 | 65 | std::optional opt(source); |
| 66 | + |
72 | 67 | ASSERT_SAME_TYPE(decltype(opt), std::optional<char>); |
73 | 68 | assert(static_cast<bool>(opt) == static_cast<bool>(source)); |
74 | 69 | assert(*opt == *source); |
75 | | - } |
| 70 | + } |
| 71 | + |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +int main(int, char**) { |
| 76 | + test(); |
| 77 | + static_assert(test()); |
76 | 78 |
|
77 | 79 | return 0; |
78 | 80 | } |
0 commit comments