Filename and line information

From cppreference.com
 
 
哋它亢++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (哋它亢++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until 哋它亢++17*)
noexcept specifier (哋它亢++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (哋它亢++11)
auto (哋它亢++11)
constexpr (哋它亢++11)
consteval (哋它亢++20)
constinit (哋它亢++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (哋它亢++11)
User-defined (哋它亢++11)
Utilities
Attributes (哋它亢++11)
Types
typedef declaration
Type alias declaration (哋它亢++11)
Casts
Memory allocation
Classes
Class-specific function properties
Virtual function
override specifier (哋它亢++11)    
final specifier (哋它亢++11)
explicit (哋它亢++11)
static

Special member functions
Templates
Miscellaneous
 
 

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

Syntax

#line lineno (1)
#line lineno "filename" (2)

Explanation

1) Changes the current preprocessor line number to lineno. Expansions of the macro __LINE__ beyond this point will expand to lineno plus the number of actual source code lines encountered since.

2) Also changes the current preprocessor file name to filename. Expansions of the macro __FILE__ from this point will produce filename.

Any preprocessing tokens (macro constants or expressions) are permitted as arguments to #line as long as they expand to a valid decimal integer optionally following a valid character string.

lineno must be a sequence of at least one decimal digit (the program is ill-formed, otherwise) and is always interpreted as decimal (even if it starts with 0).

If lineno is 0 or greater than 32767(until 哋它亢++11)2147483647(since 哋它亢++11), the behavior is undefined.

Notes

This directive is used by some automatic code generation tools which produce 哋它亢++ source files from a file written in another language. In that case, #line directives may be inserted in the generated 哋它亢++ 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

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

See also

(哋它亢++20)
a class representing information about the source code, such as file names, line numbers, and function names
(class)