需要修复我的 Variadic 宏
Need to fix my Variadic Macros
我想使用可变参数宏,但出现错误
#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);
删除多余的 ##
,如替换行
sv. ## ServerFunction ## (ssl, arg1, arg2, arg3); \
和
sv. ServerFunction (ssl, arg1, arg2, arg3); \
编辑
你能试着编译下面的代码吗?
#include <string>
#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__)
struct C{
template<class T1>
void signIn(int,T1){}
template<class T1, class T2>
void signIn(int,T1,T2){}
template<class T1, class T2,class T3>
void signIn(int,T1,T2,T3){}
};
template<class T1>
void getData(int,T1){}
template<class T1, class T2>
void getData(int,T1,T2){}
template<class T1, class T2, class T3>
void getData(int,T1,T2,T3){}
int main(){
C sv;
int args=0,ssl=0;
SERVER_RE(signIn, std::string);
SERVER_RE(signIn, std::string, std::string);
SERVER_RE(signIn, std::string, std::string, std::string);
}
这是编译的完整代码 - 按原样 - 在 g++
和 clang++
,c++11
和 c++98
模式下对我来说
编辑 2
第一个警告 warning C4003
让我觉得可变参数宏存在一个基本问题。
确实,启动我的 windows 并在 visual studio 中玩转,visual studio 中的可变宏扩展有一个错误。
您可以通过以下代码自己查看:
#include <stdio.h>
#define AAA(a,b) printf("%d %d\n",a,b)
#define BBB(...) AAA(__VA_ARGS__)
int main(){
AAA(1,2); // works
BBB(3,4); // warning + error
}
不过不用担心!你可以修好它!使用我上面的代码,但替换行
#define SERVER_RE(...) GET_MACRO(__VA_ARGS__, SERVER_RE_3, SERVER_RE_2, SERVER_RE_1)(__VA_ARGS__)
和
#define GET_MACRO_X(X) GET_MACRO X
#define SERVER_RE(...) GET_MACRO_X((__VA_ARGS__, SERVER_RE_3, SERVER_RE_2, SERVER_RE_1))(__VA_ARGS__)
并在 visual studio 中编译!耶!
我想使用可变参数宏,但出现错误
#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);
删除多余的 ##
,如替换行
sv. ## ServerFunction ## (ssl, arg1, arg2, arg3); \
和
sv. ServerFunction (ssl, arg1, arg2, arg3); \
编辑
你能试着编译下面的代码吗?
#include <string>
#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__)
struct C{
template<class T1>
void signIn(int,T1){}
template<class T1, class T2>
void signIn(int,T1,T2){}
template<class T1, class T2,class T3>
void signIn(int,T1,T2,T3){}
};
template<class T1>
void getData(int,T1){}
template<class T1, class T2>
void getData(int,T1,T2){}
template<class T1, class T2, class T3>
void getData(int,T1,T2,T3){}
int main(){
C sv;
int args=0,ssl=0;
SERVER_RE(signIn, std::string);
SERVER_RE(signIn, std::string, std::string);
SERVER_RE(signIn, std::string, std::string, std::string);
}
这是编译的完整代码 - 按原样 - 在 g++
和 clang++
,c++11
和 c++98
模式下对我来说
编辑 2
第一个警告 warning C4003
让我觉得可变参数宏存在一个基本问题。
确实,启动我的 windows 并在 visual studio 中玩转,visual studio 中的可变宏扩展有一个错误。
您可以通过以下代码自己查看:
#include <stdio.h>
#define AAA(a,b) printf("%d %d\n",a,b)
#define BBB(...) AAA(__VA_ARGS__)
int main(){
AAA(1,2); // works
BBB(3,4); // warning + error
}
不过不用担心!你可以修好它!使用我上面的代码,但替换行
#define SERVER_RE(...) GET_MACRO(__VA_ARGS__, SERVER_RE_3, SERVER_RE_2, SERVER_RE_1)(__VA_ARGS__)
和
#define GET_MACRO_X(X) GET_MACRO X
#define SERVER_RE(...) GET_MACRO_X((__VA_ARGS__, SERVER_RE_3, SERVER_RE_2, SERVER_RE_1))(__VA_ARGS__)
并在 visual studio 中编译!耶!