C++ __COUNTER__ Definition

≡放荡痞女 提交于 2019-12-22 08:59:39

问题


I have two versions of a c++ compiler installed on my computer. One of them recognizes the __COUNTER__ macro and the other does not. After doing some research to make the program compile in both I have yet to come across the Macro definition for __COUNTER__. Is this some special Macro done by the compiler or can I copy the definition for __COUNTER__ into my source code, if I can copy it what is the code I need.


回答1:


__COUNTER__ is a built-in in several common compilers. It is not possible to define manually. If you're stuck with a compiler that doesn't support it, your best option might be to run your code through a preprocessor that does support it before feeding it into the compiler.




回答2:


It's a special macro which has been introduced by Visual Studio and I think is now supported by GCC too.

It basically provides a unique counter over integral numbers which can be used to generate unique identifiers.

From GCC release notes:

A new predefined macro __COUNTER__ has been added. It expands to sequential integral values starting from 0. In conjunction with the ## operator, this provides a convenient means to generate unique identifiers.

If you don't have it available to a compiler you can easily mimic the behavior with a static variable. But I'm not sure what you are compiling so I'm not sure how this counter is used in the code you have available.



来源:https://stackoverflow.com/questions/20768379/c-counter-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!