C++ Logo

std-proposals

Advanced search

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

From: Alejandro Colomar <une+cxx_std-proposals_at_[hidden]>
Date: Thu, 19 Feb 2026 15:59:23 +0100
Hi Ville,

On 2026-02-19T16:14:32+0200, Ville Voutilainen via Std-Proposals wrote:
> I have a rather fundamental question as the first question:
>
> so, is it the case that
> 1) when the condition can be statically proven true, the program just compiles
> 2) when the condition can be statically proven false, the compilation fails
> 3) when the condition cannot be proven either way, the linking fails
> ?

A viable GCC implementation would be

 #define compiler_assert(e) do \
 { \
  [[gnu::error("")]] extern void fail_(void); \
                                                                      \
  if (!(e)) \
   fail_(); \
 } while (0)

1) The program just compiles
2) The compilation fails
3) The compilation fails

The difference with static_assert(3) is that the failure happens in
a later stage of the compiler, after optimizations, which means we are
not restricted to integer constant expressions. But the linker is not
needed.


Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es>

Received on 2026-02-19 14:59:28