| 1 |
// PR c++/113629 |
| 2 |
// { dg-do compile { target c++23 } } |
| 3 |
|
| 4 |
template <typename T> constexpr bool is_lvalue = false; |
| 5 |
template <typename T> constexpr bool is_lvalue<T&> = true; |
| 6 |
|
| 7 |
struct A { |
| 8 |
constexpr operator bool(this auto&& self) { |
| 9 |
return is_lvalue<decltype(self)>; |
| 10 |
} |
| 11 |
}; |
| 12 |
|
| 13 |
constexpr A a; |
| 14 |
static_assert(static_cast<bool>(a)); |
| 15 |
static_assert((bool)a); |
| 16 |
static_assert(!static_cast<bool>(A{})); |
| 17 |
static_assert(!(bool)A{}); |
| 18 |
|
| 19 |
struct B : A {}; |
| 20 |
|
| 21 |
constexpr B b; |
| 22 |
static_assert(static_cast<bool>(b)); |
| 23 |
static_assert((bool)b); |
| 24 |
static_assert(!static_cast<bool>(B{})); |
| 25 |
static_assert(!(bool)B{}); |
| 26 |
|
| 27 |
struct C { |
| 28 |
template <typename R, typename T> |
| 29 |
explicit constexpr operator R(this T&&) { |
| 30 |
return is_lvalue<T>; |
| 31 |
} |
| 32 |
}; |
| 33 |
|
| 34 |
constexpr C c; |
| 35 |
static_assert(static_cast<bool>(c)); |
| 36 |
static_assert((bool)c); |
| 37 |
static_assert(!static_cast<bool>(C{})); |
| 38 |
static_assert(!(bool)C{}); |
| 39 |
|
| 40 |
struct D { |
| 41 |
explicit constexpr operator bool(this const D&) { return true; } |
| 42 |
explicit constexpr operator bool(this const D&&) { return false; } |
| 43 |
}; |
| 44 |
|
| 45 |
constexpr D d; |
| 46 |
static_assert(static_cast<bool>(d)); |
| 47 |
static_assert((bool)d); |
| 48 |
static_assert(!static_cast<bool>(D{})); |
| 49 |
static_assert(!(bool)D{}); |