1//===----------------------------------------------------------------------===//
2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3// See https://llvm.org/LICENSE.txt for license information.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
8// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
9
10// template<class U, class... Args>
11// constexpr T& emplace(initializer_list<U> il, Args&&... args) noexcept;
12// Constraints: is_nothrow_constructible_v<T, initializer_list<U>&, Args...> is true.
13//
14// Effects: Equivalent to:
15// if (has_value()) {
16// destroy_at(addressof(val));
17// } else {
18// destroy_at(addressof(unex));
19// has_val = true;
20// }
21// return *construct_at(addressof(val), il, std::forward<Args>(args)...);
22
23#include <algorithm>
24#include <cassert>
25#include <concepts>
26#include <expected>
27#include <type_traits>
28#include <utility>
29
30#include "../../types.h"
31#include "test_macros.h"
32
33template <class T, class... Args>
34concept CanEmplace = requires(T t, Args&&... args) { t.emplace(std::forward<Args>(args)...); };
35
36static_assert(CanEmplace<std::expected<int, int>, int>);
37
38template <bool Noexcept>
39struct CtorFromInitializerList {
40 CtorFromInitializerList(std::initializer_list<int>&) noexcept(Noexcept);
41 CtorFromInitializerList(std::initializer_list<int>&, int) noexcept(Noexcept);
42};
43
44static_assert(CanEmplace<std::expected<CtorFromInitializerList<true>, int>, std::initializer_list<int>&>);
45static_assert(!CanEmplace<std::expected<CtorFromInitializerList<false>, int>, std::initializer_list<int>&>);
46static_assert(CanEmplace<std::expected<CtorFromInitializerList<true>, int>, std::initializer_list<int>&, int>);
47static_assert(!CanEmplace<std::expected<CtorFromInitializerList<false>, int>, std::initializer_list<int>&, int>);
48
49struct Data {
50 std::initializer_list<int> il;
51 int i;
52
53 constexpr Data(std::initializer_list<int>& l, int ii) noexcept : il(l), i(ii) {}
54};
55
56constexpr bool test() {
57 // has_value
58 {
59 auto list1 = {1, 2, 3};
60 auto list2 = {4, 5, 6};
61 std::expected<Data, int> e(std::in_place, list1, 5);
62 decltype(auto) x = e.emplace(list2, 10);
63 static_assert(std::same_as<decltype(x), Data&>);
64 assert(&x == &(*e));
65
66 assert(e.has_value());
67 assert(std::ranges::equal(e.value().il, list2));
68 assert(e.value().i == 10);
69 }
70
71 // !has_value
72 {
73 auto list = {4, 5, 6};
74 std::expected<Data, int> e(std::unexpect, 5);
75 decltype(auto) x = e.emplace(list, 10);
76 static_assert(std::same_as<decltype(x), Data&>);
77 assert(&x == &(*e));
78
79 assert(e.has_value());
80 assert(std::ranges::equal(e.value().il, list));
81 assert(e.value().i == 10);
82 }
83
84 // TailClobberer
85 {
86 std::expected<TailClobberer<0>, bool> e(std::unexpect);
87 auto list = {4, 5, 6};
88 e.emplace(list);
89 assert(e.has_value());
90 }
91
92 return true;
93}
94
95int main(int, char**) {
96 test();
97 static_assert(test());
98 return 0;
99}
100

source code of libcxx/test/std/utilities/expected/expected.expected/assign/emplace.intializer_list.pass.cpp