【发布时间】:2015-05-31 06:57:25
【问题描述】:
我想使用可变参数宏,但出现错误
#define SERVER_RE_1(ServerFunction, Type1) \
{ \
Type1 arg1; \
getData(args, arg1); \
sv. ## ServerFunction ## (ssl, arg1); \
}
#define SERVER_RE_2(ServerFunction, Type1, Type2) \
{ \
Type1 arg1; \
Type2 arg2; \
getData(args, arg1, arg2); \
sv. ## ServerFunction ## (ssl, arg1, arg2); \
}
#define SERVER_RE_3(ServerFunction, Type1, Type2, Type3) \
{ \
Type1 arg1; \
Type2 arg2; \
Type3 arg3; \
getData(args, arg1, arg2, arg3); \
sv. ## ServerFunction ## (ssl, arg1, arg2, arg3); \
}
#define GET_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define SERVER_RE(...) GET_MACRO(__VA_ARGS__, SERVER_RE_3, SERVER_RE_2, SERVER_RE_1)(__VA_ARGS__)
-
SERVER_RE(signIn, std::string, std::string);
错误 C2065:“signIn”:未声明的标识符
错误 C2275: 'std::string' : 非法使用这种类型作为表达式
-
但是 SERVER_RE_2 效果很好。
SERVER_RE2(signIn, std::string, std::string);
【问题讨论】:
-
您在使用第一个示例时在哪里声明了
signIn? -
是sv的函数。它在 sv 类的头文件中声明。
标签: c++ c++11 macros variadic-macros