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 17:30:21 +0100
On 2026-02-19T16:58:42+0100, Jan Schultke wrote:
> >
> > It also doesn't work at all, because next to no user of this facility
> > wants the contract evaluation semantic configuration to turn this
> > assert completely off.
> >
>
> Maybe you don't want it, but it sounds a lot like OP wants something along
> those lines. As Jonathan said, when __OPTIMIZE__ is not defined, all the
> compile_asserts go away.
>
> That's exactly the kind of global configuration that I'm thinking of. Of
> course, you may want to make decisions differently for different contract
> assertions/preconditions, and there are proposals that allow for that.
>
>
> > And they certainly don't want the act of turning off runtime checks to
> > also turn off all compile-time checks.
> >
>
> But people doing that is the status quo. Ignore semantic with contracts
> gets rid of any runtime check, and also offers no compile-time check (other
> than static analysis in the future). The new "proof" semantic would allow
> you to turn off runtime checks and turn them into compile-time checks.
>
> Of course, once again, there is still the issue of granularity; you can
> only switch globally, and that's presumably going to be fixed in C++29.

People don't need to switch it globally:

 #ifdef __OPTIMIZE__
  compile_assert(foo);
 #endif

With that, you can selectively handle each case differently.


Cheers,
Alex

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

Received on 2026-02-19 16:30:36