| 1 |
// { dg-do run { target c++17 } } |
| 2 |
|
| 3 |
#include <optional> |
| 4 |
#include <testsuite_hooks.h> |
| 5 |
|
| 6 |
constexpr void |
| 7 |
test_convert_contained_value_to_bool() |
| 8 |
{ |
| 9 |
struct False { constexpr operator bool() const { return false; } }; |
| 10 |
|
| 11 |
False f; |
| 12 |
std::optional<False> o = f; |
| 13 |
|
| 14 |
// Should use optional(const optional<U>&) ctor, not optional(U&&): |
| 15 |
std::optional<bool> o2 = o; |
| 16 |
|
| 17 |
// Contained value should be static_cast<bool>(f) not static_cast<bool>(o): |
| 18 |
VERIFY( o2.value() == false ); |
| 19 |
|
| 20 |
std::optional<False> o3; |
| 21 |
std::optional<const bool> o4 = o3; |
| 22 |
// Should have no contained value, not static_cast<bool>(o3): |
| 23 |
VERIFY( ! o4.has_value() ); |
| 24 |
} |
| 25 |
|
| 26 |
constexpr void |
| 27 |
test_convert_contained_value_to_bool_explicit() |
| 28 |
{ |
| 29 |
struct False { constexpr explicit operator bool() const { return false; } }; |
| 30 |
|
| 31 |
False f; |
| 32 |
std::optional<False> o = f; |
| 33 |
|
| 34 |
// Should use optional(const optional<U>&) ctor, not optional(U&&): |
| 35 |
std::optional<bool> o2(o); |
| 36 |
|
| 37 |
// Contained value should be static_cast<bool>(f) not static_cast<bool>(o): |
| 38 |
VERIFY( o2.value() == false ); |
| 39 |
|
| 40 |
std::optional<False> o3; |
| 41 |
std::optional<const bool> o4(o3); |
| 42 |
// Should have no contained value, not static_cast<bool>(o3): |
| 43 |
VERIFY( ! o4.has_value() ); |
| 44 |
} |
| 45 |
|
| 46 |
int main() |
| 47 |
{ |
| 48 |
test_convert_contained_value_to_bool(); |
| 49 |
test_convert_contained_value_to_bool_explicit(); |
| 50 |
|
| 51 |
#if __cpp_lib_optional >= 202106 |
| 52 |
static_assert([] { |
| 53 |
test_convert_contained_value_to_bool(); |
| 54 |
test_convert_contained_value_to_bool_explicit(); |
| 55 |
return true; |
| 56 |
}()); |
| 57 |
#endif |
| 58 |
} |