Find current line number in C++
This post will discuss how to find the current line number in C++.
1. Using __LINE__ Macro
The standard solution to find the current line number in C++ is using the predefined macro __LINE__. It returns the integer value representing the current line in the source code file being compiled.
|
1 2 3 4 5 6 7 8 |
#include <iostream> int main() { std::cout << __LINE__ << std::endl; // 5 return 0; } |
The preprocessor will simply replace the __LINE__ macro with the current line number. This might cause undesired results when used within a function, as shown below.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> void log() { std::cout << __LINE__ << std::endl; // 4 } int main() { log(); return 0; } |
To print the line number of the caller function, we have to pass __LINE__ as a function argument.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> void log(int line) { std::cout << line << std::endl; // 9 } int main() { log(__LINE__); return 0; } |
2. Using std::experimental::source_location
The std::experimental::source_location class represents certain information about the source code, such as file names, line numbers, and function names. The source_location class is defined in the header <experimental/source_location> and provides a better alternative to macros.
The current() function constructs a new source_location object and line() function to returns the line number represented by it. It can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> #include <experimental/source_location> int main() { using std::experimental::source_location; std::cout << source_location::current().line() << std::endl; // 7 return 0; } |
To print the line number of the caller function, we can use the current() function as a default argument.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <experimental/source_location> using std::experimental::source_location; void log(const source_location loc = source_location::current()) { std::cout << loc.line() << std::endl; // 11 } int main() { log(); return 0; } |
3. Using std::source_location
If your compiler supports C++20, then the new standard <source_location> header is a better alternative. With C++20, the std::experimental::source_location was merged into the mainline ISO C++ standard as std::source_location. All its member functions and usage remains the same.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <source_location> void log(const std::source_location loc = std::source_location::current()) { std::cout << loc.line() << std::endl; // 12 } int main() { std::cout << std::source_location::current().line() << std::endl; // 10 log(); return 0; } |
That’s all about finding the current line number in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)