1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // <optional> |
10 | // UNSUPPORTED: c++03, c++11, c++14 |
11 | |
12 | // template<class T> |
13 | // optional(T) -> optional<T>; |
14 | |
15 | #include <optional> |
16 | #include <cassert> |
17 | |
18 | #include "test_macros.h" |
19 | |
20 | struct A {}; |
21 | |
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 | } |
32 | |
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 | } |
39 | |
40 | { |
41 | // optional(const T&); |
42 | 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 | } |
48 | |
49 | { |
50 | // optional(T*); |
51 | 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 | } |
57 | |
58 | { |
59 | // optional(T[]); |
60 | int source[] = {1, 2, 3}; |
61 | std::optional opt(source); |
62 | ASSERT_SAME_TYPE(decltype(opt), std::optional<int*>); |
63 | assert(static_cast<bool>(opt)); |
64 | assert((*opt)[0] == 1); |
65 | } |
66 | |
67 | // Test the implicit deduction guides |
68 | { |
69 | // optional(optional); |
70 | std::optional<char> source('A'); |
71 | std::optional opt(source); |
72 | ASSERT_SAME_TYPE(decltype(opt), std::optional<char>); |
73 | assert(static_cast<bool>(opt) == static_cast<bool>(source)); |
74 | assert(*opt == *source); |
75 | } |
76 | |
77 | return 0; |
78 | } |
79 | |