3

I run cl /P test.cpp, the file and result is as following.

test.cpp

#define FiltedLog( ...) \
    if (logDetail) \
        MP_LOG(LOG_INFO,  __VA_ARGS__);

#define MP_LOG(level,fmt,...) \
    BOOAT::LOG("MP", level, fmt, ##__VA_ARGS__)

#define LOG(tag,level,fmt,...) \
    Log::log(tag, level, "%s: " fmt, __PRETTY_FUNCTION__, ##__VA_ARGS__)

int main ()
{
     FiltedLog ( "abc", 1, 2);
}

Cl /P test.cpp :

#line 1 "test.cpp"

int main ()
{
     if (logDetail) BOOAT::Log::log("MP", LOG_INFO, "%s: " "abc", 1, 2, __PRETTY_FUNCTION__ );;
}

I wonder why the __PRETTY_FUNCTION__ are put as the last arguments in the result. I assume the result should be:

 if (logDetail) BOOAT::Log::log("MP", LOG_INFO, "%s: " "abc", __PRETTY_FUNCTION__, 1, 2);;

Is it an bug of VS 2010?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ZijingWu
  • 3,350
  • 3
  • 25
  • 40

1 Answers1

11

Yes, this is a longstanding bug in the Visual C++ preprocessor. To work around it, use indirection:

#define INDIRECT_EXPAND(m, args) m args

#define FiltedLog( ...) \
    if (logDetail) \
        INDIRECT_EXPAND(MP_LOG, (LOG_INFO, __VA_ARGS__));

(Do note that __PRETTY_FUNCTION__ is a nonstandard extension that is not supported by Visual C++.)

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 1
    can you give more information about this bug? For example, when this bug will happen. And why this workaround works. – ZijingWu Feb 19 '14 at 02:51