Visual studio __VA_ARGS__ issue

Deadly 提交于 2019-12-20 15:34:04

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/21869917/visual-studio-va-args-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!