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