c++ - Need to fix my Variadic Macros -
i want use variadic macros errors
#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); error c2065: 'signin' : undeclared identifier
error c2275: 'std::string' : illegal use of type expression
-
but server_re_2 works good.
server_re2(signin, std::string, std::string);
remove ##, in replace lines
sv. ## serverfunction ## (ssl, arg1, arg2, arg3); \ with
sv. serverfunction (ssl, arg1, arg2, arg3); \ edit
can try compile following code?
#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); } this full code compiles - - me in both g++ , clang++, in both c++11 , c++98 modes
edit 2
that first warning warning c4003 makes me think there basic problem variadic macros here.
indeed, booting windows , playing around in visual studio, there bug in visual studio in variadic macro expantion.
you can see following code:
#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 } but worry not! can fix it! use above code, replace line
#define server_re(...) get_macro(__va_args__, server_re_3, server_re_2, server_re_1)(__va_args__) with
#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__) and compiles in visual studio! yay!
Comments
Post a Comment