0

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());   

    ...
}
4

1 回答 1

0

您可以执行以下操作:

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

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

DEBUG( obj1.tostring() + " some stuff " + obj2.tostring() )
于 2019-02-23T04:49:00.417 回答