C++ Logo

std-proposals

Advanced search

Re: [std-proposals] compile_assert draft proposal for feedback

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Sun, 22 Feb 2026 12:35:15 +0000
>
>
>
> On Sun, 22 Feb 2026, 11:26 Marcin Jaczewski via Std-Proposals, <
> std-proposals_at_[hidden]> wrote:


>
> How this is better than possible:
>
> ```
> contract_assert [[ prove_this_at_compile_time ]] (expr);
> ```
>
> or even better make it precondition:
>
> ```
> void foo() pre [[ prove_this_at_compile_time ]] (expr)
> {
> }
> ```
>
> And have a special compilation mode that try enforce this at compilation
> time,
> and in other cases it fallback to normal contract handling.
>


Yes, I think "make it ill-formed at compile-time when optimizing, otherwise
add a runtime assertion" would be something people want to do.

With the current proposal, that's awkward. You would need to do:

#ifdef __OPTIMIZING__ && __ENABLE_COMPILE_ASSERT__
  compile_assert(cond);
#else
  assert(cond);
#endif

or wrap that into your own macro. But that depends on a non-standard macro
which is compiler-dependent. It would be easier if the feature defined
something that you could check to see if the compile_assert is actually
enabled.


Also, the more I look at code examples using this, the less I like the
name. It didn't tell me what it does. All assertions are compiled, what is
a "compile assert"? Assert that something compiles, i.e. is syntactically
valid? That's what a requires expression does, but it's not what this does.



> Beside preconditions are visible on both sides of function call even if not
> inlined, this means you do not need to rely on opimalzier to do this job
> as you put enough preconditions to make all checks local.
>
>

Received on 2026-02-22 12:35:33