宏C++;问题__ 什么(如果有的话)是C++应用程序中的一些潜在问题? 内联函数是更合适的解决方案吗 #define EVENT_INFO(_format_, ...) CMyEvent::Generate(__FILE__, __LINE__, CMyEvent::EVT_HIGH, _format_, __VA_ARGS__) void CMyEvent::Generate( const char* file, // filename int line, // line number CMyEvent::LEVEL level, // severity level const char *format, // format of the msg / data ...) // variable arguments { // Get a message from the pool CMyEvent* p_msg = GetMessageFromPool(); if(p_msg != NULL) { va_list arguments; // points to each unnamed argument va_start(arguments, format); // Fill the object with strings and data. p_msg->Fill(file, line, level, 0, format, arguments); va_end(arguments); } }

宏C++;问题__ 什么(如果有的话)是C++应用程序中的一些潜在问题? 内联函数是更合适的解决方案吗 #define EVENT_INFO(_format_, ...) CMyEvent::Generate(__FILE__, __LINE__, CMyEvent::EVT_HIGH, _format_, __VA_ARGS__) void CMyEvent::Generate( const char* file, // filename int line, // line number CMyEvent::LEVEL level, // severity level const char *format, // format of the msg / data ...) // variable arguments { // Get a message from the pool CMyEvent* p_msg = GetMessageFromPool(); if(p_msg != NULL) { va_list arguments; // points to each unnamed argument va_start(arguments, format); // Fill the object with strings and data. p_msg->Fill(file, line, level, 0, format, arguments); va_end(arguments); } },c++,macros,C++,Macros,如果您可以将消息重写为使用operator,则Variatic宏非常适合用于日志记录。比使用iostreams要好得多。内联函数将无法工作,除非您愿意手动传递\uuuu文件\uuuuuuu,\uuuu行\uuuuu 可以用“安全”(不必要的IMHO)< /P> > P>来附上格式> /P> > P>。在C++中,可以避免使用变量列表的陷阱,这些变量列表存在许多问题: 不检查参数的数量 不检查参数类型 >让它更像C++,做一些类似的事情: #define EVENT_INFO(args) E

如果您可以将消息重写为使用operator,则Variatic宏非常适合用于日志记录。比使用iostreams要好得多。内联函数将无法工作,除非您愿意手动传递
\uuuu文件\uuuuuuu
\uuuu行\uuuuu


可以用“安全”(不必要的IMHO)< /P> > P>来附上<强>格式> /P> > P>。在C++中,可以避免使用变量列表的陷阱,这些变量列表存在许多问题:

  • 不检查参数的数量
  • 不检查参数类型
<> >让它更像C++,做一些类似的事情:

#define EVENT_INFO(args) EventLogStream (__FILE__, __LINE__, __PRETTY_FUNCTION__) << args

\define EVENT\u INFO(args)EventLogStream(\uuuu FILE\uuuuuuuuuu,\uuuuu LINE\uuuuuuuuuuuu,\uuuuu PRETTY\u FUNCTION\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu)首先将
\uu format\uuuuuuuuu
放入宏体的括号中。宏可以轻松删除生产构建的代码。我定义,并在我的流中打印:logger
EVENT_INFO ("The answer to " << the_question << " is " << answer); // usually 42
class Vector3D
{
   EventLogStream &operator << (EventLogStream &out) { out << "{" << x << ", " << y << ", " << z << "}"; }
}