Debian Codegraph @

libcxx/test/std/utilities/expected/expected.void/dtor.pass.cpp

raw
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 ~expected();
11 //
12 // Effects: If has_value() is false, destroys unex.
13 //
14 // Remarks: If is_trivially_destructible_v<E> is true, then this destructor is a trivial destructor.
15
16 #include <cassert>
17 #include <expected>
18 #include <type_traits>
19 #include <utility>
20
21 #include "test_macros.h"
22
23 // Test Remarks: If is_trivially_destructible_v<E> is true, then this destructor is a trivial destructor.
24 struct NonTrivial {
25 ~NonTrivial() {}
26 };
27
28 static_assert(std::is_trivially_destructible_v<std::expected<void, int>>);
29 static_assert(!std::is_trivially_destructible_v<std::expected<void, NonTrivial>>);
30
31 struct TrackedDestroy {
32 bool& destroyed;
33 constexpr TrackedDestroy(bool& b) : destroyed(b) {}
34 constexpr ~TrackedDestroy() { destroyed = true; }
35 };
36
37 constexpr bool test() {
38 // has value
39 { [[maybe_unused]] std::expected<void, TrackedDestroy> e(std::in_place); }
40
41 // has error
42 {
43 bool errorDestroyed = false;
44 { [[maybe_unused]] std::expected<void, TrackedDestroy> e(std::unexpect, errorDestroyed); }
45 assert(errorDestroyed);
46 }
47
48 return true;
49 }
50
51 int main(int, char**) {
52 test();
53 static_assert(test());
54 return 0;
55 }