Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro C++ Issues __VA_ARGS__

Tags:

c++

macros

What (if any) are some potential problems with a C++ macro usage like this? Would an inline function be a more appropriate solution?

#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);
    }
}
like image 492
CodeLizard Avatar asked Jul 04 '26 02:07

CodeLizard


1 Answers

As you're using C++, you can avoid the pitfalls of using variable argument lists which are subject to many problems:

  • No check on quantity of arguments
  • No check on argument type

To make it more C++, do something like:

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

and invoke it like (warning: all code here is pseudocode and may be syntactically incorrect, but you should get the basic idea):

EVENT_INFO ("The answer to " << the_question << " is " << answer); // usually 42

The EventLogStream is similar to the cout object and like cout, you can provide class specific output:

class Vector3D
{
   EventLogStream &operator << (EventLogStream &out) { out << "{" << x << ", " << y << ", " << z << "}"; }
}
like image 133
Skizz Avatar answered Jul 06 '26 20:07

Skizz