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 | // UNSUPPORTED: c++03, c++11, c++14 |
10 | // <optional> |
11 | |
12 | // template <class T, class U, class... Args> |
13 | // constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args); |
14 | |
15 | #include <cassert> |
16 | #include <memory> |
17 | #include <optional> |
18 | #include <string> |
19 | |
20 | #include "test_macros.h" |
21 | |
22 | struct TestT { |
23 | int x; |
24 | int size; |
25 | int *ptr; |
26 | constexpr TestT(std::initializer_list<int> il) |
27 | : x(*il.begin()), size(static_cast<int>(il.size())), ptr(nullptr) {} |
28 | constexpr TestT(std::initializer_list<int> il, int *p) |
29 | : x(*il.begin()), size(static_cast<int>(il.size())), ptr(p) {} |
30 | }; |
31 | |
32 | constexpr bool test() |
33 | { |
34 | { |
35 | auto opt = std::make_optional<TestT>({42, 2, 3}); |
36 | ASSERT_SAME_TYPE(decltype(opt), std::optional<TestT>); |
37 | assert(opt->x == 42); |
38 | assert(opt->size == 3); |
39 | assert(opt->ptr == nullptr); |
40 | } |
41 | { |
42 | int i = 42; |
43 | auto opt = std::make_optional<TestT>({42, 2, 3}, &i); |
44 | ASSERT_SAME_TYPE(decltype(opt), std::optional<TestT>); |
45 | assert(opt->x == 42); |
46 | assert(opt->size == 3); |
47 | assert(opt->ptr == &i); |
48 | } |
49 | return true; |
50 | } |
51 | |
52 | int main(int, char**) |
53 | { |
54 | test(); |
55 | static_assert(test()); |
56 | { |
57 | auto opt = std::make_optional<std::string>({'1', '2', '3'}); |
58 | assert(*opt == "123" ); |
59 | } |
60 | { |
61 | auto opt = std::make_optional<std::string>({'a', 'b', 'c'}, std::allocator<char>{}); |
62 | assert(*opt == "abc" ); |
63 | } |
64 | return 0; |
65 | } |
66 | |