xref: /llvm-project/libcxx/test/std/utilities/expected/expected.void/dtor.pass.cpp (revision 6a54dfbfe534276d644d7f9c027f0deeb748dd53)
1e356f681SHui Xie //===----------------------------------------------------------------------===//
2*6a54dfbfSLouis Dionne //
3e356f681SHui Xie // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e356f681SHui Xie // See https://llvm.org/LICENSE.txt for license information.
5e356f681SHui Xie // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e356f681SHui Xie //
7e356f681SHui Xie //===----------------------------------------------------------------------===//
8e356f681SHui Xie 
9e356f681SHui Xie // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10e356f681SHui Xie 
11e356f681SHui Xie // constexpr ~expected();
12e356f681SHui Xie //
13e356f681SHui Xie // Effects: If has_value() is false, destroys unex.
14e356f681SHui Xie //
15e356f681SHui Xie // Remarks: If is_trivially_destructible_v<E> is true, then this destructor is a trivial destructor.
16e356f681SHui Xie 
17e356f681SHui Xie #include <cassert>
18e356f681SHui Xie #include <expected>
19e356f681SHui Xie #include <type_traits>
20e356f681SHui Xie #include <utility>
21e356f681SHui Xie 
22e356f681SHui Xie #include "test_macros.h"
23e356f681SHui Xie 
24e356f681SHui Xie // Test Remarks: If is_trivially_destructible_v<E> is true, then this destructor is a trivial destructor.
25e356f681SHui Xie struct NonTrivial {
26e356f681SHui Xie   ~NonTrivial() {}
27e356f681SHui Xie };
28e356f681SHui Xie 
29e356f681SHui Xie static_assert(std::is_trivially_destructible_v<std::expected<void, int>>);
30e356f681SHui Xie static_assert(!std::is_trivially_destructible_v<std::expected<void, NonTrivial>>);
31e356f681SHui Xie 
32e356f681SHui Xie struct TrackedDestroy {
33e356f681SHui Xie   bool& destroyed;
34e356f681SHui Xie   constexpr TrackedDestroy(bool& b) : destroyed(b) {}
35e356f681SHui Xie   constexpr ~TrackedDestroy() { destroyed = true; }
36e356f681SHui Xie };
37e356f681SHui Xie 
38e356f681SHui Xie constexpr bool test() {
39e356f681SHui Xie   // has value
40e356f681SHui Xie   { [[maybe_unused]] std::expected<void, TrackedDestroy> e(std::in_place); }
41e356f681SHui Xie 
42e356f681SHui Xie   // has error
43e356f681SHui Xie   {
44e356f681SHui Xie     bool errorDestroyed = false;
45e356f681SHui Xie     { [[maybe_unused]] std::expected<void, TrackedDestroy> e(std::unexpect, errorDestroyed); }
46e356f681SHui Xie     assert(errorDestroyed);
47e356f681SHui Xie   }
48e356f681SHui Xie 
49e356f681SHui Xie   return true;
50e356f681SHui Xie }
51e356f681SHui Xie 
52e356f681SHui Xie int main(int, char**) {
53e356f681SHui Xie   test();
54e356f681SHui Xie   static_assert(test());
55e356f681SHui Xie   return 0;
56e356f681SHui Xie }
57