Date: Fri, 20 Feb 2026 15:47:24 +0100
On a side note, this isn't the first time that someone proposes a feature
that depends on optimizer knowledge in some way.
Perhaps we should cut out the middle-man and standardize something along
the lines of __builtin_constant_p. The only issue I have with
__builtin_constant_p is that there's no guarantee that the condition holds,
even if __builtin_constant_p identifies it as constant, as in:
if (__builtin_constant_p(f()) && f())
It's possible that through bad luck, after a few optimization passes,
__builtin_constant_p(x) optimizes to true, and no more optimization happens
past that point, and a runtime invocation of f() happens. It is also
possible that f() gets inlined on the left side but not on the right side,
so the runtime check takes place.
Anyway, I think any proposal should consider the option of simply
standardizing some construct like
if provable (f()) { /* ... */ }
This provides a superset of the proposed functionality, like:
#define compile_assert(...) \
if provable (!(__VA_ARGS__)) { /* somehow produce error here */ }
I wouldn't want to standardize half a dozen features closely related to
__builtin_constant_p if we could just expose that. At least, a proposal
should consider that option.
On Fri, 20 Feb 2026 at 12:45, Jonathan Wakely via Std-Proposals <
std-proposals_at_[hidden]> wrote:
>
>
> On Fri, 20 Feb 2026 at 11:33, Bjorn Reese via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> On 2/19/26 15:59, Alejandro Colomar via Std-Proposals wrote:
>>
>> > A viable GCC implementation would be
>>
>> Isn't it sufficient and portable to call assert() in a constexpr
>> context? For example
>>
>> constexpr void compiler_assert(bool condition) {
>> assert(condition);
>> }
>>
>> I am aware that the above does not generate a useful error message, but
>> that can be remedied.
>>
>
> No, that requires the condition to be a constant expression. The proposed
> feature only requires it to be a "compile-time constant" after compiler
> optimizations, which is a weaker requirement.
>
> int i = 1;
> static_assert(1); // not a constant expression
> compile_assert(1); // might be constant, with optimization
>
> Whether something is a constant expression is formally defined by the C++
> standard.
>
> Whether something is a compile-time constant depends on the compiler
> performing value propagation, common subexpression elimination, etc.
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
that depends on optimizer knowledge in some way.
Perhaps we should cut out the middle-man and standardize something along
the lines of __builtin_constant_p. The only issue I have with
__builtin_constant_p is that there's no guarantee that the condition holds,
even if __builtin_constant_p identifies it as constant, as in:
if (__builtin_constant_p(f()) && f())
It's possible that through bad luck, after a few optimization passes,
__builtin_constant_p(x) optimizes to true, and no more optimization happens
past that point, and a runtime invocation of f() happens. It is also
possible that f() gets inlined on the left side but not on the right side,
so the runtime check takes place.
Anyway, I think any proposal should consider the option of simply
standardizing some construct like
if provable (f()) { /* ... */ }
This provides a superset of the proposed functionality, like:
#define compile_assert(...) \
if provable (!(__VA_ARGS__)) { /* somehow produce error here */ }
I wouldn't want to standardize half a dozen features closely related to
__builtin_constant_p if we could just expose that. At least, a proposal
should consider that option.
On Fri, 20 Feb 2026 at 12:45, Jonathan Wakely via Std-Proposals <
std-proposals_at_[hidden]> wrote:
>
>
> On Fri, 20 Feb 2026 at 11:33, Bjorn Reese via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> On 2/19/26 15:59, Alejandro Colomar via Std-Proposals wrote:
>>
>> > A viable GCC implementation would be
>>
>> Isn't it sufficient and portable to call assert() in a constexpr
>> context? For example
>>
>> constexpr void compiler_assert(bool condition) {
>> assert(condition);
>> }
>>
>> I am aware that the above does not generate a useful error message, but
>> that can be remedied.
>>
>
> No, that requires the condition to be a constant expression. The proposed
> feature only requires it to be a "compile-time constant" after compiler
> optimizations, which is a weaker requirement.
>
> int i = 1;
> static_assert(1); // not a constant expression
> compile_assert(1); // might be constant, with optimization
>
> Whether something is a constant expression is formally defined by the C++
> standard.
>
> Whether something is a compile-time constant depends on the compiler
> performing value propagation, common subexpression elimination, etc.
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Received on 2026-02-20 14:47:39
