Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does a new __LINE__ start?

I do not understand the output of the following program:

#include <iostream>

#define FOO std::cout << __LINE__ << ' ' \
                      << __LINE__ << '\n';
int main()
{
    FOO

    std::cout << __LINE__ << ' ' \
              << __LINE__ << '\n';
}

The first output is 7 and 7, indicating that the expansion of FOO is a single logical line, but the second output is 9 and 10, indicating two distinct logical lines. Why is there a difference?

like image 336
fredoverflow Avatar asked Nov 27 '10 10:11

fredoverflow


People also ask

What is __ LINE __ in C++?

__LINE__ is a preprocessor macro that expands to current line number in the source file, as an integer. __LINE__ is useful when generating log statements, error messages intended for programmers, when throwing exceptions, or when writing debugging code.

What does __ file __ mean in C?

__FILE__ This macro expands to the name of the current input file, in the form of a C string constant. This is the path by which the preprocessor opened the file, not the short name specified in ' #include ' or as the input file name argument. For example, "/usr/local/include/myheader.

What does __ LINE __ return?

The __LINE__ is an inbuilt Macro in C programming language, it returns current line number of the code.

What does #line mean in C?

The #line directive tells the preprocessor to set the compiler's reported values for the line number and filename to a given line number and filename.


3 Answers

Because

1:  #include <iostream>
2: 
3:  #define FOO std::cout << __LINE__ << ' ' \
4:                        << __LINE__ << '\n';
5:  int main()
6:  {
7:      FOO // the first two __LINE__s come from here, that's one line of code
8: 
9:      std::cout << __LINE__ << ' ' \ // 3rd __LINE__ comes from here
10:              << __LINE__ << '\n'; // 4th __LINE__ comes from here
11: }

__LINE__ expands to physical lines, not logical lines:

The line number of the current source line is one greater than the number of new-line characters read or introduced in translation phase 1 (2.2) while processing the source file to the current token.

While the lines ended by \ are concatenated in translation phase 2.

The other only logical implementation would be to print 3 and 4 for the invocation of FOO, but that seems not very useful.

You can also look at this the following way: __LINE__ is not any different from any other macro. It's just updated automatically by the compiler at the beginning of every line. So the code is kind of interpreted this way:

#include <iostream>

#define __LINE__ 3
#define FOO std::cout << __LINE__ << ' ' \
                      << __LINE__ << '\n';
int main()
{
#define __LINE__ 7
    FOO

#define __LINE__ 9
    std::cout << __LINE__ << ' ' \ // Yeah, you're right
#define __LINE__ 10
             << __LINE__ << '\n';
}

This is not valid code, but it demonstrates how the things work. Apply the usual macro expansion rules and you'll get the output you've got.

like image 165
Yakov Galka Avatar answered Oct 16 '22 10:10

Yakov Galka


Because #define expansion contains hackery to make sure __LINE__ is the one where the macro is "invoked". Otherwise a lot of error messages would make no sense to the user.

like image 34
Jakob Borg Avatar answered Oct 16 '22 11:10

Jakob Borg


cause one you define in #define statement which always evaluated as one line. However, the second case is really two lines of code.

like image 2
Vladimir Ivanov Avatar answered Oct 16 '22 10:10

Vladimir Ivanov