Testing if an expression has side effects at compile time

Martin Uecker muecker@gwdg.de
Fri Jan 9 14:20:24 GMT 2026


Am Freitag, dem 09.01.2026 um 14:11 +0000 schrieb Richard Earnshaw (foss) via Gcc:
> On 09/01/2026 13:47, Alejandro Colomar via Gcc wrote:
> > Hi Florian,
> > 
> > On Fri, Jan 09, 2026 at 02:37:52PM +0100, Florian Weimer wrote:
> > > * Alejandro Colomar via Gcc:
> > > 
> > > > If there's no way, it would be interesting to add compound literals of
> > > > function type to achieve this:
> > > > 
> > > > 	#define strnulD(...)                                          \
> > > > 	((static inline auto                                          \
> > > > 	  (auto s))                                                   \
> > > > 	{                                                             \
> > > > 		return s + strlen(s);                                 \
> > > > 	}(__VA_ARGS__))
> > > 
> > > This already works:
> > > 
> > > static inline auto
> > > strnulD (auto s)
> > > {
> > >   return s + strlen (s);
> > > }
> > 
> > Sorry; I forgot to mention I mean in C, not C++.
> > 
> > For a second, you got me illusioned and thought that might have been
> > implemented in C.  :-)
> > 
> > 	alx@devuan:~/tmp$ cat strnul.c 
> > 	#include <string.h>
> > 
> > 	static inline auto
> > 	strnulD(auto s)
> > 	{
> > 		return s + strlen(s);
> > 	}
> > 
> > 	int
> > 	main(void)
> > 	{
> > 		const char *p = strnulD("foo");
> > 	}
> > 	alx@devuan:~/tmp$ gcc strnul.c 
> > 	strnul.c:4:14: error: storage class specified for parameter ‘s’
> > 	    4 | strnulD(auto s)
> > 	      |              ^
> > 	strnul.c:3:1: error: ‘auto’ requires a plain identifier, possibly with attributes, as declarator
> > 	    3 | static inline auto
> > 	      | ^~~~~~
> > 	strnul.c: In function ‘main’:
> > 	strnul.c:12:25: error: implicit declaration of function ‘strnulD’ [-Wimplicit-function-declaration]
> > 	   12 |         const char *p = strnulD("foo");
> > 	      |                         ^~~~~~~
> > 	strnul.c:12:25: error: initialization of ‘const char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
> > 
> > > No need to invent new syntax for it.
> > 
> > Any chance we can get this in GNU C?
> > 
> > 
> 
> C23 has auto in C.
> 
Yes, but a much more limited form. Above function is a template
and I do not really would want to have template in C.

Martin



More information about the Gcc mailing list