我正在尝试为日志记录机制编写宏。 我编写了一个可变参数宏,但它不适用于std::string。 该代码如下所示:
#include
#include
#define LOG_NOTE(m, ...) printf(m, ##__VA_ARGS__)
int main()
{
std::string foo ="random string";
int bar = 5;
LOG_NOTE("%s %d %s","Hello World", bar, foo);
return 0;
}
如果我像下面这样调用宏,则不会出现任何错误。
LOG_NOTE("%s %d %s","Hello World", bar,"random string");
编译器输出:
In function 'int main()': 5:49: error: cannot pass objects of
non-trivially-copyable type 'std::string {aka class
std::basic_string}' through '...' 11:5: note: in expansion of
macro 'LOG_NOTE'
顺便说一句,如果您使用gcc的-E标志来获取预处理器之后的输出,则在编写宏时会很有帮助。
您是否需要格式,还是只是用空格分隔的值?
我实际上只需要用空格分隔的值,目前我正在尝试根据答案实施它,但是我收到了链接器错误
您不能将对象传递给printf,因此当前必须使用
LOG_NOTE("%s %d %s","Hello World", bar, foo.c_str());
如果您不需要格式化,只需将每个参数都用空格隔开,则可以简单地使用可变参数模板而不是MACRO:
template
void LOG_NOTE(const Ts&...args)
{
const char* sep ="";
(((std::cout << sep << args), sep =""), ...); // C++17 folding expression
// C++11/C++14 version are more verbose:
// int dummy[] = {0, ((std::cout << sep << args), (sep =""), 0)...};
// static_cast(dummy); // avoid warning for unused variable
}
int main()
{
std::string foo ="random string";
int bar = 5;
LOG_NOTE("Hello World", bar, foo);
}
演示版
感谢您的输入@ Jarod42,但我已经为C ++ 17找到了类似的答案。但是,我正在寻找头和源文件分开的C ++ 11示例。
template不是在头文件和cpp文件之间拆分的最佳选择。
添加了C ++ 11版本。
这里的问题不是可变参数宏,而是对printf的调用。 看一下文档:格式说明符"%s"对应于char*,而不是std::string。 printf仅能处理原始的内置类型。 您可以将调用更改为
LOG_NOTE("%s %d %s","Hello World", bar, foo.c_str());
解决这个问题。
或者,使用增强格式。它确实支持std::string和许多其他用户定义的类型。
I wrote a variadic macro
别。 使用可变参数模板函数。
您遇到的实际问题是,您试图通过C API(printf)传递C ++对象(std::string)。 这是不可能的。
您需要某种转换机制,例如:
#include
#include
template
decltype(auto) convert_for_log_note(T const& x)
{
return x;
}
decltype(auto) convert_for_log_note(std::string const& x)
{
return x.c_str();
}
template
void LOG_NOTE(const char* format, Args&&...args)
{
printf(format, convert_for_log_note(args)...);
}
int main()
{
std::string foo ="random string";
int bar = 5;
LOG_NOTE("%s %d %s
","Hello World", bar, foo);
return 0;
}
输出示例:
Hello World 5 random string
http://coliru.stacked-crooked.com/a/beb3431114833860
公平地说,日志宏是众所周知的"无宏"规则的免除项,原因有以下三个:__FILE__,__LINE__以及在发行版本中将其转换为"不执行"语句的能力。您不能消除函数调用参数的副作用。
只要C ++委员会能够表现自己,@ MSalters很快就会拥有std :: source_location,它将与if constexpr一起完全消除对宏的需求。请注意,只要您通过通用引用传递参数,参数的副作用就不会成为问题。显然传递LOG("%s%",mything.to_string()。c_str());确实会带来副作用,因此应避免使用。最好为convert_for_log_note(MyThing const&)类型提供ADL重载。