Difference between revisions of "cpp/preprocessor/line"
m (The format of an assert is implementation defined) |
m (→Top: ~wording) |
||
Line 2: | Line 2: | ||
{{cpp/preprocessor/navbar}} | {{cpp/preprocessor/navbar}} | ||
− | Changes the current file name | + | Changes the source code's line number and, optionally, the current file name, in the preprocessor. |
===Syntax=== | ===Syntax=== | ||
− | |||
{{sdsc begin}} | {{sdsc begin}} | ||
− | {{sdsc | num=1 | {{ttb|#line}} {{spar|lineno}}}} | + | {{sdsc|num=1|{{ttb|#line}} {{spar|lineno}}}} |
− | {{sdsc | num=2 | | + | {{sdsc|num=2|{{ttb|#line}} {{spar|lineno}} {{ttb|"}}{{spar|filename}}{{ttb|"}}}} |
{{sdsc end}} | {{sdsc end}} | ||
===Explanation=== | ===Explanation=== | ||
− | |||
1) Changes the current preprocessor line number to {{spar|lineno}}. Expansions of the macro {{lc|__LINE__}} beyond this point will expand to {{spar|lineno}} plus the number of actual source code lines encountered since. | 1) Changes the current preprocessor line number to {{spar|lineno}}. Expansions of the macro {{lc|__LINE__}} beyond this point will expand to {{spar|lineno}} plus the number of actual source code lines encountered since. | ||
Line 27: | Line 25: | ||
===Example=== | ===Example=== | ||
− | {{example | p=true | code= | + | {{example|p=true|code= |
#include <cassert> | #include <cassert> | ||
#define FNAME "test.cc" | #define FNAME "test.cc" | ||
Line 35: | Line 33: | ||
assert(2+2 == 5); | assert(2+2 == 5); | ||
} | } | ||
− | + | |output= | |
test: test.cc:777: int main(): Assertion `2+2 == 5' failed. | test: test.cc:777: int main(): Assertion `2+2 == 5' failed. | ||
}} | }} | ||
===References=== | ===References=== | ||
− | |||
{{ref std c++23}} | {{ref std c++23}} | ||
− | {{ref std | section=15.7 | title=Line control | id=cpp.line}} | + | {{ref std|section=15.7|title=Line control|id=cpp.line}} |
{{ref std end}} | {{ref std end}} | ||
{{ref std c++20}} | {{ref std c++20}} | ||
− | {{ref std | section=15.7 | title=Line control | id=cpp.line}} | + | {{ref std|section=15.7|title=Line control|id=cpp.line}} |
{{ref std end}} | {{ref std end}} | ||
{{ref std c++17}} | {{ref std c++17}} | ||
− | {{ref std | section=19.4 | title=Line control | id=cpp.line}} | + | {{ref std|section=19.4|title=Line control|id=cpp.line}} |
{{ref std end}} | {{ref std end}} | ||
{{ref std c++14}} | {{ref std c++14}} | ||
− | {{ref std | section=16.4 | title=Line control | id=cpp.line}} | + | {{ref std|section=16.4|title=Line control|id=cpp.line}} |
{{ref std end}} | {{ref std end}} | ||
{{ref std c++11}} | {{ref std c++11}} | ||
− | {{ref std | section=16.4 | title=Line control | id=cpp.line}} | + | {{ref std|section=16.4|title=Line control|id=cpp.line}} |
{{ref std end}} | {{ref std end}} | ||
{{ref std c++98}} | {{ref std c++98}} | ||
− | {{ref std | section=16.4 | title=Line control | id=cpp.line}} | + | {{ref std|section=16.4|title=Line control|id=cpp.line}} |
{{ref std end}} | {{ref std end}} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/utility/dsc source_location}} | + | {{dsc inc|cpp/utility/dsc source_location}} |
− | {{dsc see c | c/preprocessor/line | Filename and line information | nomono=true}} | + | {{dsc see c|c/preprocessor/line|Filename and line information|nomono=true}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|cs|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|cs|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 11:49, 1 December 2022
Changes the source code's line number and, optionally, the current file name, in the preprocessor.
Contents |
[edit] Syntax
#line lineno
|
(1) | ||||||||
#line lineno " filename"
|
(2) | ||||||||
[edit] 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 C++11)2147483647
(since C++11), the behavior is undefined.
[edit] 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.
[edit] 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.
[edit] 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]
[edit] See also
(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
|