C++ Logo

std-proposals

Advanced search

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

From: Jonathan Grant <jg_at_[hidden]>
Date: Tue, 10 Mar 2026 21:14:50 +0000
On 10/03/2026 20:07, Tony V E via Std-Proposals wrote:
> Should the name be closer to
>
> optimizer_assert()
>
> Or something like that?
>
>
>
> Be seeing you,
> Tony

Hi Tony

Thank you for taking a look at P4021.

The proposed name is a very good question, when I first wrote it in 2023, names like optimizer_assert() were among those considered before settling on compile_assert(). It's right that you have spotted my sample implementation in use relies upon the optimizer for constant propagation and dead code removal.

In my latest edit I have tightened the wording, so compile_assert() provides syntax for expressing constraints intended to be validated at compile time. The mechanism is intentionally left implementation-defined; of course static analysis or optimizer-based reasoning would be the ways to consider the control-flow and failure cases. That is branches that should be unreachable, branch pruning those that are not reachable.

May I ask if you could give it a try in your projects? I'm particularly interested in feedback from clang and MSVC users who are using it for bounds checking buffer accesses. I've got a testsuite of examples
https://github.com/jonnygrant/compile_assert/tree/main/testsuite

Best regards
Jonathan

Received on 2026-03-10 21:14:54