Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is __COUNTER__ macro portable?

I have a piece of code which uses __COUNTER__ macro to generate unique names for variables.

Is this code portable ? I know that GCC and MSVS support it. What's about other compilers ? Is the macro defined by standard (as far as I know before C++14 it wasn't).

like image 289
tommyk Avatar asked Oct 02 '14 11:10

tommyk


3 Answers

It's definitely not standard.

It's a compiler extension (GNU C extensions)

The common predefined macros are GNU C extensions.

and a Microsoft-specific one,

Microsoft-Specific Predefined Macros:

__ COUNTER __

also supported by clang as language extension.

The standard doesn't mention it anywhere.

like image 91
Marco A. Avatar answered Oct 22 '22 00:10

Marco A.


GCC manual, section Common Predefined Macros, states

The common predefined macros are GNU C extensions.

I've also never seen this macro in the C99, C11 or C++11 standards.

As for practical portability: Clang supports it, too.

like image 45
Fred Foo Avatar answered Oct 22 '22 00:10

Fred Foo


It seems NO. When I ctrl+f "__COUNTER__" on standard pdf, I couldn't find anything >o<

like image 1
ikh Avatar answered Oct 22 '22 01:10

ikh