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