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// GCC crashes on this file, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120577
16// XFAIL: gcc-15
17
18#include <cassert>
19#include <memory>
20#include <optional>
21#include <string>
22
23#include "test_macros.h"
24
25struct TestT {
26 int x;
27 int size;
28 int *ptr;
29 constexpr TestT(std::initializer_list<int> il)
30 : x(*il.begin()), size(static_cast<int>(il.size())), ptr(nullptr) {}
31 constexpr TestT(std::initializer_list<int> il, int *p)
32 : x(*il.begin()), size(static_cast<int>(il.size())), ptr(p) {}
33};
34
35constexpr bool test()
36{
37 {
38 auto opt = std::make_optional<TestT>({42, 2, 3});
39 ASSERT_SAME_TYPE(decltype(opt), std::optional<TestT>);
40 assert(opt->x == 42);
41 assert(opt->size == 3);
42 assert(opt->ptr == nullptr);
43 }
44 {
45 int i = 42;
46 auto opt = std::make_optional<TestT>({42, 2, 3}, &i);
47 ASSERT_SAME_TYPE(decltype(opt), std::optional<TestT>);
48 assert(opt->x == 42);
49 assert(opt->size == 3);
50 assert(opt->ptr == &i);
51 }
52 return true;
53}
54
55int main(int, char**)
56{
57 test();
58 static_assert(test());
59 {
60 auto opt = std::make_optional<std::string>({'1', '2', '3'});
61 assert(*opt == "123");
62 }
63 {
64 auto opt = std::make_optional<std::string>({'a', 'b', 'c'}, std::allocator<char>{});
65 assert(*opt == "abc");
66 }
67 return 0;
68}
69

source code of libcxx/test/std/utilities/optional/optional.specalg/make_optional_explicit_initializer_list.pass.cpp