C/C++如何在引号之间将宏参数扩展为文本

Ric*_*ich 3 c c++ macros

这就是我所拥有的(message()是来自第三方库的专用日志记录功能):

#define LOG(fmt, ...) message("%s %s(): #fmt", __FILE__, __func__, __VA_ARGS__);
Run Code Online (Sandbox Code Playgroud)

所以我希望能够做到这样的事情:

LOG("Hello world")
LOG("Count = %d", count)
Run Code Online (Sandbox Code Playgroud)

并将它扩展到:

message("%s %s(): Hello world", __FILE__, __func__);
message("%s %s(): Count = %d", __FILE__, __func__, count);
Run Code Online (Sandbox Code Playgroud)

但#fmt的东西不起作用.它不会评估宏参数并打印为"#fmt".有可能做我想做的事吗?

Nic*_*ick 5

不要#fmt加入报价.只需使用字符串文字串联来连接两个文字.

#define LOG(fmt, ...) message("%s %s(): " fmt, __FILE__, __func__, __VA_ARGS__);
Run Code Online (Sandbox Code Playgroud)