开发者

c++ macro concatation not worked under gcc

#include <iostream>
v开发者_StackOverflow社区oid LOG_TRACE() { std::cout << "reach here"; }

#define LOG_LL_TRACE    LOG_TRACE
#define LL_TRACE    0
#define __LOG(level)  LOG_##level()
#define LOG(level) __LOG(##level)

int main()
{
    LOG(LL_TRACE);
    return 0;
}

The code is worked under Visual Studio, but report: test.cpp:13:1: error: pasting "(" and "LL_TRACE" does not give a valid preprocessing token.

How can I fix it?

ps: The macro expansion is supposed to be LOG(LL_TRACE) --> __LOG(LL_TRACE) --> LOG_LL_TRACE().

ps: suppose LL_TRACE must have a 0 value, do not remove it.


Two things make this code not compile on g++: First, the error you're quoting is because you want to have this:

#define LOG(level) __LOG(level)

Notice no ##. Those hashmarks mean concatenate, but you're not concatenating anything. Just forwarding an argument.

The second error is that you have to remove

#define LL_TRACE    0

This line means you end up calling LOG(0) which expands into LOG_0 which isn't defined.


Shouldn't it be :

#define LOG(level) __LOG(level)

That works:

#include <iostream>
void LOG_TRACE() { std::cout << "reach here"; }
#define LOG_LL_TRACE    LOG_TRACE
#define __LOG( level ) LOG_##level()                                                    
#define LOG(level) __LOG(level)

int main()
{
        LOG( LL_TRACE );                                     
        return 0;                                                           
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜