At what point in the preprocessing phase does __LINE__ expand?

I have a function in the header file:

template <int line>
inline void log() {}

And then I try this trick to make it easier to use:

#define LOG_LINE log<__LINE__>()

And then in the .cpp file I:

void main()
{
    LOG_LINE;
}

And it looks like it works the way I would like. I get the line from the .cpp file, not the line in which it is LOG_LINEdeclared in the .h file. But I don’t understand how it works. Does preprocessing do dual-processing C ++, leaving behind special macros such as __LINE__for the second pass? Is this a portable (standard) behavior? Should I expect this to work with all major C ++ compilers? So far I have only tried MSVC.

+4
source share
3

, . , .

, #define , , . , .

, , ( ). .. ( ).

.

. N3797 16.3. . , , .

+3

, , , . , , , .

, LOG_LINE log<__LINE__>(), __LINE__ 3, log<3>() - .

+5

This does not require double pass pretreatment. We are talking about the extension order of nested macros (well, in a sense, the expression expands in as many passes as necessary to expand all nested macros). Quote from the answer to this question :

All arguments that do not appear after the # operator or any of the ## sides expand completely when they are replaced, and not before a functionally similar macro expands.

+2
source

Source: https://habr.com/ru/post/1530597/


All Articles