Spec-Zone.ru › C++

Filename and line information

Changes the source code's line number and, optionally, the current file name, in the preprocessor.

Syntax

#line lineno new-line (1)
#line lineno "filename" new-line (2)
#line pp-tokens new-line (3)
1) Changes the current preprocessor line number to lineno.
2) Also changes the current preprocessor file name to filename.
3) If neither (1) nor (2) is matched, pp-tokens will undergo macro replacement. The directive after replacement will be tried to match with (1) or (2) again.
new-line - The new-line character
lineno - A sequence of one or more decimal digits (0 to 9), optional single quotes (') may be inserted between the digits as a separator(since C++14)
filename - A sequence of one or more s-char s (see string literal)
pp-tokens - A sequence of one or more preprocessing tokens

Explanation

Expansions of the macro __LINE__ beyond a #line directive will expand to lineno plus the number of actual source code lines encountered since. If filename is provided, expansions of the macro __FILE__ from this point will produce filename .

If the decimal number specified by lineno is 0 or greater than 32767(until C++11)2147483647(since C++11), the behavior is undefined(until C++26)the program is ill-formed(since C++26).

Notes

This directive is used by some automatic code generation tools which produce C++ source files from a file written in another language. In that case, #line directives may be inserted in the generated C++ file referencing line numbers and the file name of the original (human-editable) source file.

Example

#include <cassert>
#define FNAME "test.cc"
int main()
{
#line 777 FNAME
        assert(2+2 == 5);
}

Possible output:

test: test.cc:777: int main(): Assertion `2+2 == 5' failed.

References

  • C++23 standard (ISO/IEC 14882:2024):
    • 15.7 Line control [cpp.line]
  • C++20 standard (ISO/IEC 14882:2020):
    • 15.7 Line control [cpp.line]
  • C++17 standard (ISO/IEC 14882:2017):
    • 19.4 Line control [cpp.line]
  • C++14 standard (ISO/IEC 14882:2014):
    • 16.4 Line control [cpp.line]
  • C++11 standard (ISO/IEC 14882:2011):
    • 16.4 Line control [cpp.line]
  • C++98 standard (ISO/IEC 14882:1998):
    • 16.4 Line control [cpp.line]

See also

source_location
(C++20)
a class representing information about the source code, such as file names, line numbers, and function names
(class)
C documentation for Filename and line information

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/preprocessor/line

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API