繁体   English   中英

具有可变参数的C ++宏

[英]C++ macro with variable arguments

1. #define debug(...) printf( __VA_ARGS__)

2. #define debug(...) std::cout<< __VA_ARGS__

显然,1可以,2会在编译时出错。 是否可以将“ std :: cout”与可变参数一起使用?

这个宏有什么意义?

'debug'宏用于打印某些内容以调试代码。

void test(const classtype1 &obj1,const classtype2 &obj2)
{
    // rewrite operator<< 
    debug(obj1,obj2); 

    //if use printf, I must call tostring method(or something likes that) to 
    //series the object to string. 
    debug(obj1.tostring(),obj2.tostring());   

    ...
}

您可以执行以下操作:

#define DEBUG(x) do { std::osacquire( std::cerr ) << __FILE__ << ":" << __LINE__ << " " << x << std::endl; } while (0);

然后在任何要使用宏的地方:

DEBUG( obj1.tostring() + " some stuff " + obj2.tostring() )

暂无
暂无

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

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