Question

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?

Was it helpful?

Solution

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++.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top