16,471
社区成员
发帖
与我相关
我的任务
分享
#define MyDLLInterface(ret, output, input, ...) \
do \
{ \
std::cout << "in ..." << std::endl; \
ret = DLLInterface(output, input, ##__VA_ARGS__); \
std::cout << "out ..." << std::endl; \
} while(false)
#include <iostream>
int DLLInterface(char * output, const char * input, ...)
{
std::cout << "ok" << std::endl;
return(0);
}
#define MyDLLInterface(output, input, ...) \
{ \
std::cout << "in ..." << std::endl; \
DLLInterface(output, input, ##__VA_ARGS__); \
std::cout << "out ..." << std::endl; \
}
int main(int argc, char * argv[])
{
char output[1];
const char * input = "%s %d";
MyDLLInterface(output, input, "test", 1);
return(0);
}
用宏来做的话,VS2010,GCC编译器都是支持的//变长模板参数,需要C++11支持
template<class ... T>
int func( T... v )
{
puts("------");
int r = printf( v... );
puts("======");
return r;
}
//变长参数宏,不一定所有编译器都支持
#define FUNC( ... ) \
{ \
puts("macro ------"); \
printf( __VA_ARGS__ ); \
puts("macro ======"); \
}
int main()
{
func( "%d,%f,%c\n" , 1,2.0 , 'c' );
FUNC( "%d,%f,%c\n" , 1,2.0 , 'c' );
return 0;
}
其他方案都有缺陷...