Testing if an expression has side effects at compile time

Alejandro Colomar alx@kernel.org
Fri Jan 9 15:45:11 GMT 2026


Hi Ben,

On Fri, Jan 09, 2026 at 10:35:24AM -0500, Ben Boeckel wrote:
> 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.

Because string APIs don't get constant expressions, I don't think it
would work in this case.  But a similar builtin might work.


Have a lovely day!
Alex

> 
> --Ben

-- 
<https://www.alejandro-colomar.es>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20260109/2430229e/attachment-0001.sig>


More information about the Gcc mailing list