std::source_location in C++ 20
Last Updated :
14 Jul, 2023
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.
Similar Reads
std::quoted in C++ 14 std::quoted is an I/O manipulator function that was introduced in C++ 14 as the part of <iomanip> library. Its primary purpose is to handle the quoted string in the input and output operations. In this article, we will learn about the std::quoted manipulator, how it works and how to use it in
5 min read
Modules in C++ 20 In C++20, the concept of modules was introduced to improve the efficiency of the compilation process and provide better organization and encapsulation of code. Modules allow developers to divide their code into separate components, each with its own interface and implementation, and import them as n
6 min read
C++ 20 - std::format C++20 comes with a host of new features and improvements, including the format() function. In this article, we will explore how std::format can be used to format strings in C++20. C++20 - std::format std::format is a new function Introduced in C++20 that provides a way to format strings by replacing
4 min read
Can References Refer to Invalid Location in C++? Reference Variables: You can create a second name for a variable in C++, which you can use to read or edit the original data contained in that variable. While this may not sound appealing at first, declaring a reference and assigning it a variable allows you to treat the reference as if it were the
2 min read
Scope Resolution Operator in C++ In C++, the scope resolution operator (::) is used to access the identifiers such as variable names and function names defined inside some other scope in the current scope. Let's take a look at an example:C++#include <iostream> int main() { // Accessing cout from std namespace using scope // r
4 min read