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