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: Thu, 19 Feb 2026 15:43:16 +0000
On Thu, 19 Feb 2026 at 15:37, Ville Voutilainen <ville.voutilainen_at_[hidden]>
wrote:

> On Thu, 19 Feb 2026 at 17:33, Jonathan Wakely via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> > void f(int* p) {
> > compiler_assert(p != nullptr);
> > }
> >
> > Even with optimizations turned up to maximum, this will always be
> ill-formed.
>
> Sure. And then
>
> void f(int* p) {
> some_mandatory_runtime_assert(p != nullptr);
> compiler_assert(p != nullptr);
> }
>
> can make it well-formed.
>

Or just:

[[assume(p)]];
compiler_assert(p != nullptr);

Which can (sometimes, depending on optimizations) also work if the assume
is in the caller of f, but only if all calls to f are inlined.

[[gnu::always_inline]]
inline void f(int* p) {
  compiler_assert(p != nullptr);
}

void g(int p)
{
  f(&p); // ok, address is non-null
}

void h(int* p)
{
  [[assume(p)]];
  f(p); // ok, address assumed non-null
}

Received on 2026-02-19 15:43:33