Date: Sun, 05 Apr 2026 17:13:19 +0000
Hello,
Yesterday I ran into really annoying behavior with GCC with this sort of code:
consteval bool is_defined() {
const auto is_complete = is_complete_type(^^to_define);
return is_complete;
}
consteval {
define_aggregate(^^to_define, {});
}
static_assert(is_complete_type(^^to_define));
static_assert(is_defined());
With the experimental Clang reflection branch, both static asserts succeed, but with GCC, then the final one does not: https://godbolt.org/z/TdsxWs3PW
It does, however, succeed if you leave the 'const' off of the 'is_complete' variable.
GCC seems to me to be taking advantage of the "potentially usable in constant expressions" (https://eel.is/c++draft/expr.const#def:potentially_usable_in_constant_expressions) feature and lifting the `is_complete` variable to be effectively 'constexpr', and evaluating 'is_complete_type(^^to_define)' right away. This will happen even in a template because GCC can see that the expression doesn't rely on any dependent names, and so will still eagerly evaluate it.
But in short, I'm wondering if this is at all standard behavior? It really really feels like it should be a bug, that something so innocuous as a 'const' specifier could so starkly change the behavior of your program. The problem is that I'm not sure that it is actually a bug in GCC.
Any clarification towards that would be greatly appreciated.
Thank you
Yesterday I ran into really annoying behavior with GCC with this sort of code:
consteval bool is_defined() {
const auto is_complete = is_complete_type(^^to_define);
return is_complete;
}
consteval {
define_aggregate(^^to_define, {});
}
static_assert(is_complete_type(^^to_define));
static_assert(is_defined());
With the experimental Clang reflection branch, both static asserts succeed, but with GCC, then the final one does not: https://godbolt.org/z/TdsxWs3PW
It does, however, succeed if you leave the 'const' off of the 'is_complete' variable.
GCC seems to me to be taking advantage of the "potentially usable in constant expressions" (https://eel.is/c++draft/expr.const#def:potentially_usable_in_constant_expressions) feature and lifting the `is_complete` variable to be effectively 'constexpr', and evaluating 'is_complete_type(^^to_define)' right away. This will happen even in a template because GCC can see that the expression doesn't rely on any dependent names, and so will still eagerly evaluate it.
But in short, I'm wondering if this is at all standard behavior? It really really feels like it should be a bug, that something so innocuous as a 'const' specifier could so starkly change the behavior of your program. The problem is that I'm not sure that it is actually a bug in GCC.
Any clarification towards that would be greatly appreciated.
Thank you
Received on 2026-04-05 17:13:28
