Date: Sat, 6 Dec 2025 01:51:51 +0000
It will take me a few more days to finish the paper on 'chimeric_ptr',
but while writing it, I've come across another problem.
When we use "if constexpr" with "requires", we can end up with code
like the following:
if constexpr ( requires { p = &static_cast<Interface&>(*q); } )
{
p = &static_cast<Interface&>(*q);
}
The body of the 'if' statement is duplicated in the 'if' condition. We
could do away with the duplication as follows:
if compiles
{
p = &static_cast<Interface&>(*q);
}
For now, here's one clunky way to avoid the duplication:
#define IF_COMPILES(...) \
if constexpr (requires { __VA_ARGS__; }) { \
__VA_ARGS__; \
}
I had tried experimenting with a lambda as in the following code, but
it doesn't compile:
auto try_static = [&]<class Dummy = void>() constexpr
{
pI = __builtin_addressof(
chimeric_detail::static_upcast<Interface&>(*p2) );
};
if constexpr (requires { try_static(); })
{
try_static();
}
The compiler bails out when the body of the lambda fails to compile. I
thought having the template parameter 'Dummy' would prevent this, but
alas it doesn't.
I wonder is it at all conceivable that we could have a core language
feature, "if compiles"?
but while writing it, I've come across another problem.
When we use "if constexpr" with "requires", we can end up with code
like the following:
if constexpr ( requires { p = &static_cast<Interface&>(*q); } )
{
p = &static_cast<Interface&>(*q);
}
The body of the 'if' statement is duplicated in the 'if' condition. We
could do away with the duplication as follows:
if compiles
{
p = &static_cast<Interface&>(*q);
}
For now, here's one clunky way to avoid the duplication:
#define IF_COMPILES(...) \
if constexpr (requires { __VA_ARGS__; }) { \
__VA_ARGS__; \
}
I had tried experimenting with a lambda as in the following code, but
it doesn't compile:
auto try_static = [&]<class Dummy = void>() constexpr
{
pI = __builtin_addressof(
chimeric_detail::static_upcast<Interface&>(*p2) );
};
if constexpr (requires { try_static(); })
{
try_static();
}
The compiler bails out when the body of the lambda fails to compile. I
thought having the template parameter 'Dummy' would prevent this, but
alas it doesn't.
I wonder is it at all conceivable that we could have a core language
feature, "if compiles"?
Received on 2025-12-06 01:51:43
