Date: Sun, 22 Feb 2026 03:38:04 +0000
On 19/02/2026 15:40, Andre Kostur wrote:
> On Thu, Feb 19, 2026 at 7:28 AM Jonathan Grant <jg_at_[hidden]> wrote:
>>
>> Hello
>>
>> On 19/02/2026 15:11, Andre Kostur wrote:
>>> Does this mean that this can/will behave differently depending on what
>>> optimization levels you may have enabled on the compiler? -O3 and it
>>> can pass the compile_assert(), -O0 and the compile_assert() fails?
>>> If so, this could be troublesome in the context of the Standard since
>>> it doesn't currently acknowledge "optimization levels".
>>
>> Hello Andre
>>
>> if __OPTIMIZE__ is not defined, the compile_assert() macros all compile out.
>
> That's already a problem for Standardization. __OPTIMIZE__ isn't a
> Standard-defined macro. One would have to get that standardized first
> before this feature could rely on it.
You're completely right Andre. Relying upon Optimization is a stumbling block. I updated the draft and just emailed with a requirement that the build set the following to enable the feature as well
#define __ENABLE_COMPILE_ASSERT__ 1
>> I've not compared eg -O1 and -O3 to know of any differences, but you're right there might be. The errors may end up very compiler and optimizer specific (gcc vs clang etc), it may not be possible to have all compile_assert() desired with a particular compiler.
>
> I've seen the optimization level influence which warnings the compiler
> will emit about code. Different optimization levels could cause the
> compiler to do deeper or shallower code analysis. Which makes this
> feature difficult to Standardize. The Standard doesn't get to assume
> that optimizations happen at all.
Ok I see, it might be that compile_assert() may fire if some functions became more complicated and it could no longer be optimized out too. If I had some real issues, I might look at refactoring into smaller files. I usually use -O2 on GCC
>> -- In which case, I would achieve the check another way (ie very close to the line of I am concerned about), or treat as a human review, and put in some mitigations and error handling by myself (and a test function to trigger a fault while running, an overflow etc and check detected), compile_assert() isn't suited to everything.
>
> Just for clarity: I'm not saying anything about the utility of this
> feature. Only that it will be a hard sell to Standardize it.
Maybe a compiler could add something like this, if they take in proposals like this that can't make it into the standard but have merit. I do have a working solution at the moment with my header file implementation.
Many thanks for reviewing Andre, I'm happy to have all feedback.
Jonathan
> On Thu, Feb 19, 2026 at 7:28 AM Jonathan Grant <jg_at_[hidden]> wrote:
>>
>> Hello
>>
>> On 19/02/2026 15:11, Andre Kostur wrote:
>>> Does this mean that this can/will behave differently depending on what
>>> optimization levels you may have enabled on the compiler? -O3 and it
>>> can pass the compile_assert(), -O0 and the compile_assert() fails?
>>> If so, this could be troublesome in the context of the Standard since
>>> it doesn't currently acknowledge "optimization levels".
>>
>> Hello Andre
>>
>> if __OPTIMIZE__ is not defined, the compile_assert() macros all compile out.
>
> That's already a problem for Standardization. __OPTIMIZE__ isn't a
> Standard-defined macro. One would have to get that standardized first
> before this feature could rely on it.
You're completely right Andre. Relying upon Optimization is a stumbling block. I updated the draft and just emailed with a requirement that the build set the following to enable the feature as well
#define __ENABLE_COMPILE_ASSERT__ 1
>> I've not compared eg -O1 and -O3 to know of any differences, but you're right there might be. The errors may end up very compiler and optimizer specific (gcc vs clang etc), it may not be possible to have all compile_assert() desired with a particular compiler.
>
> I've seen the optimization level influence which warnings the compiler
> will emit about code. Different optimization levels could cause the
> compiler to do deeper or shallower code analysis. Which makes this
> feature difficult to Standardize. The Standard doesn't get to assume
> that optimizations happen at all.
Ok I see, it might be that compile_assert() may fire if some functions became more complicated and it could no longer be optimized out too. If I had some real issues, I might look at refactoring into smaller files. I usually use -O2 on GCC
>> -- In which case, I would achieve the check another way (ie very close to the line of I am concerned about), or treat as a human review, and put in some mitigations and error handling by myself (and a test function to trigger a fault while running, an overflow etc and check detected), compile_assert() isn't suited to everything.
>
> Just for clarity: I'm not saying anything about the utility of this
> feature. Only that it will be a hard sell to Standardize it.
Maybe a compiler could add something like this, if they take in proposals like this that can't make it into the standard but have merit. I do have a working solution at the moment with my header file implementation.
Many thanks for reviewing Andre, I'm happy to have all feedback.
Jonathan
Received on 2026-02-22 03:38:12
