| 1 |
// { dg-do run { target c++23 } } |
| 2 |
|
| 3 |
#include <expected> |
| 4 |
#include <testsuite_hooks.h> |
| 5 |
|
| 6 |
constexpr void |
| 7 |
test_convert_contained_value_to_bool() |
| 8 |
{ |
| 9 |
struct BaseError { }; |
| 10 |
struct DerivedError : BaseError { }; |
| 11 |
|
| 12 |
std::expected<bool, DerivedError> e = false; |
| 13 |
|
| 14 |
// Should use expected(const expected<U, G>&) ctor, not expected(U&&): |
| 15 |
std::expected<bool, BaseError> e2 = e; |
| 16 |
|
| 17 |
// Contained value should be e.value() not static_cast<bool>(e): |
| 18 |
VERIFY( e2.value() == false ); |
| 19 |
|
| 20 |
std::expected<bool, DerivedError> e3(std::unexpect); |
| 21 |
std::expected<const bool, BaseError> e4 = e3; |
| 22 |
// Should have error, not static_cast<bool>(e3): |
| 23 |
VERIFY( ! e4.has_value() ); |
| 24 |
} |
| 25 |
|
| 26 |
int main() |
| 27 |
{ |
| 28 |
test_convert_contained_value_to_bool(); |
| 29 |
|
| 30 |
static_assert([] { |
| 31 |
test_convert_contained_value_to_bool(); |
| 32 |
return true; |
| 33 |
}()); |
| 34 |
} |