Date: Thu, 19 Feb 2026 16:44:37 +0100
On 19/02/2026 16:28, Jonathan Grant via Std-Proposals 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.
>
> 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.
> -- 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.
>
> Regards, Jonathan
If you want to be a bit more flexible, then you can make use of
"__builtin_constant_p()" to tell if a the compiler knows the answer to
your assert at compile-time. I use this macro:
extern void __attribute__((error("Assume failed"))) assumeFailed(void);
#define Assume(x) \
do { \
if (__builtin_constant_p(!(x))) { \
if (!(x)) { \
assumeFailed(); \
} \
} \
if (!(x)) __builtin_unreachable(); \
} while (0)
This is for optimisation, but you could use the same method to get a
fall-back to run-time assert if the compiler can't figure things out at
compile-time :
extern void __attribute__((error("Assert failed"))) assertFailed(void);
extern void runtime_assert_failed(void);
#define Assert(x) \
do { \
if (__builtin_constant_p(!(x))) { \
if (!(x)) { \
assertFailed(); \
} \
} else { \
if (!(x)) runtime_assert_failed(void); \
}
} while (0)
> 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.
>
> 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.
> -- 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.
>
> Regards, Jonathan
If you want to be a bit more flexible, then you can make use of
"__builtin_constant_p()" to tell if a the compiler knows the answer to
your assert at compile-time. I use this macro:
extern void __attribute__((error("Assume failed"))) assumeFailed(void);
#define Assume(x) \
do { \
if (__builtin_constant_p(!(x))) { \
if (!(x)) { \
assumeFailed(); \
} \
} \
if (!(x)) __builtin_unreachable(); \
} while (0)
This is for optimisation, but you could use the same method to get a
fall-back to run-time assert if the compiler can't figure things out at
compile-time :
extern void __attribute__((error("Assert failed"))) assertFailed(void);
extern void runtime_assert_failed(void);
#define Assert(x) \
do { \
if (__builtin_constant_p(!(x))) { \
if (!(x)) { \
assertFailed(); \
} \
} else { \
if (!(x)) runtime_assert_failed(void); \
}
} while (0)
Received on 2026-02-19 15:44:41
