简体   繁体   English

C++中的宏扩展

[英]Macro expansion in C++

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"这会给出编译器警告(GCC 3.2.3):“'##' 不能出现在宏扩展的任一端”

You only need ## to append a parameter to another string.你只需要## 到 append 一个参数到另一个字符串。 Your macro can be recast as您的宏可以重铸为

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

## is the concatenation operator; ##是连接运算符; the compiler is just complaining about that.编译器只是在抱怨这一点。
You cannot concatenate a token without something before it, ie 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.如果您只想初始化 singleton 指针,请从宏中删除两个##

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM