[英]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.