C++ Logo

std-proposals

Advanced search

Re: [std-proposals] compile_assert() a static assert that fires at compile time, not runtime.

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Fri, 20 Feb 2026 11:45:29 +0000
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.

Received on 2026-02-20 11:45:48