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// constexpr void emplace() noexcept;
11//
12// Effects: If has_value() is false, destroys unex and sets has_val to true.
13
14#include <cassert>
15#include <concepts>
16#include <expected>
17#include <type_traits>
18#include <utility>
19
20#include "../../types.h"
21#include "test_macros.h"
22
23template <class T>
24concept EmplaceNoexcept =
25 requires(T t) {
26 { t.emplace() } noexcept;
27 };
28static_assert(!EmplaceNoexcept<int>);
29
30static_assert(EmplaceNoexcept<std::expected<void, int>>);
31
32constexpr bool test() {
33 // has_value
34 {
35 std::expected<void, int> e;
36 e.emplace();
37 assert(e.has_value());
38 }
39
40 // !has_value
41 {
42 Traced::state state{};
43 std::expected<int, Traced> e(std::unexpect, state, 5);
44 e.emplace();
45
46 assert(state.dtorCalled);
47 assert(e.has_value());
48 }
49
50 return true;
51}
52
53int main(int, char**) {
54 test();
55 static_assert(test());
56 return 0;
57}
58

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