The latest C++ standard version 20, brought about a fresh header file called <source_location> Header. It has an efficient method of obtaining details of a function or expression’s source location while running. This functionality is immensely beneficial for debugging and profiling activities that help developers locate errors and performance problems more quickly.
std::source_location Class
The <source_location> header provides a new class called std::source_location, which has the following member functions:
- file_name(): The function or expression’s defined source file name can be obtained in the form of a std::string object upon return.
- line(): An integer that is not signed, which includes the number of the source file line where the expression or function has been defined, is returned.
- column(): The function or expression’s defined column number in the source file is included in an unsigned int return value.
- function_name(): The std::source_location object’s origin function name is returned in a std::string object.
- current(): It is a static member function that is used to create a source location object of the place where it is called in the program.
Syntax
std::source_location src = source_location_objects;
Examples of Source Location
Example 1: Program to illustrate the use of std::source_location
C++
// C++ Program to demonstrate use of source_loaction to // print function details #include <iostream> #include <source_location> using namespace std;
// function for which we check source location void foo()
{ // declaring source location object for current source
source_location src = source_location::current();
// printing details
cout << "Function: " << src.function_name() << '/n' ;
cout << "File: " << src.file_name() << "\n" ;
cout << "Line: " << src.line() << "\n" ;
cout << "Column: " << src.column() << "\n" ;
} // driver code int main()
{ foo();
return 0;
} |
Output
Function: void foo() File: main.cpp Line: 11 Column: 51
Example 2: Program to illustrate the use of source location for log messages.
C++
// C++ program to log messages with source location using // source_location header #include <iostream> #include <source_location> void log (std::string_view message,
const std::source_location& location
= std::source_location::current())
{ std::cout << location.file_name() << ":"
<< location.line() << " ("
<< location.function_name() << "): " << message
<< "\n" ;
} void foo() { log ( "Hello, world!" ); }
int main()
{ foo();
return 0;
} |
Output
main.cpp:9 (void foo()) Hello, world!
Advantages of using <source_location> Header
Advantages of using <source_location> header mentioned below:
- Easier debugging: Developers can efficiently locate the origin of a function or code with the aid of the source_location class, which offers insights such as filename, line number, column number, and function name. This data can be beneficial when dealing with obscure errors or debugging intricate code.
- Improved logging: Incorporating the source_location class into the code provides developers with elaborate logging details that pinpoint the exact location where a log message originated. This feature enables faster identification of the issue at hand, making troubleshooting more effective.
- Better profiling: Developers can use the source_location class to pinpoint the location of a function’s call, revealing potential performance bottlenecks. With this information, they can optimize their code, leading to improved application performance.