Macro expansion in C++

断了今生、忘了曾经 提交于 2020-02-15 10:01:34

问题


How can I define a macro (or a workaround for this) where the parameter is at the beginning of the line?

#define SINGLETON_IMPLEMENTATION(className) \
    ##className* ##className::instance_ = NULL;

This give a compiler warning (GCC 3.2.3): " '##' cannot appear at either end of a macro expansion"


回答1:


## is the concatenation operator; the compiler is just complaining about that.
You cannot concatenate a token without something before it, i.e. at the beginning of the macro expansion; just try to remove the ## at the beginning of the second line.
Also the second ## seems wrong. If you just want to initialize a singleton pointer, remove both ##s from your macro.




回答2:


You only need ## to append a parameter to another string. Your macro can be recast as

#define SINGLETON_IMPLEMENTATION(className) \
    className* className::instance_ = NULL;


来源:https://stackoverflow.com/questions/672618/macro-expansion-in-c

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