Testing if an expression has side effects at compile time

Ben Boeckel ben.boeckel@kitware.com
Fri Jan 9 15:35:24 GMT 2026


On Fri, Jan 09, 2026 at 13:07:34 +0100, Alejandro Colomar via Gcc wrote:
> Is there a way to implement something like this?:
> 
> 	#define strnulC(s)                           \
> 	({                                           \
> 		static_assert(!has_side_effects(s)); \
> 		s + strlen(s);                       \
> 	})

`__builtin_constant_p` might be overly restrictive, but still helpful.

--Ben


More information about the Gcc mailing list