Section

std::source_location

The class std::source_location represents source-code location information such as file names, line numbers, column numbers, and function names.

Before std::source_location, functions that needed call-site information for logging, testing, or diagnostics typically relied on predefined macros such as __LINE__ and __FILE__, so that those macros would expand in the context of the caller. std::source_location provides a library type for the same purpose.

# Declarations

struct source_location;

(since C++20)

# Semantics

  • std::source_location is a small value type that carries a snapshot of source-coordinate information.
  • A value obtained from current() represents the call site that triggered the capture, not necessarily the body of the helper that receives it.
  • The stored file name, function name, line, and column are implementation-defined observations of that source position.
  • The type is intended for diagnostics, logging, assertions, tracing, and test helpers where the call site matters.
  • std::source_location meets the DefaultConstructible, CopyConstructible, CopyAssignable, Destructible, and Swappable named requirements.
  • std::is_nothrow_move_constructible_v<std::source_location>, std::is_nothrow_move_assignable_v<std::source_location>, and std::is_nothrow_swappable_v<std::source_location> are required to be true.
  • It is intended that std::source_location remains small and cheap to copy.
  • It is unspecified whether its copy/move constructors and copy/move assignment operators are trivial or constexpr.

# Capture model

  • The usual way to create a meaningful source_location is to use std::source_location::current(), often as the default argument of a function parameter.
  • This lets a wrapper function observe the caller’s location without relying on preprocessor macros at the wrapper’s call sites.
  • A default-constructed object has unspecified values and is mainly useful as a placeholder or for later assignment.

# Representation notes

  • file_name() and function_name() return null-terminated byte strings whose exact spelling is implementation-defined.
  • line() returns the represented line number.
  • column() returns an implementation-defined offset from the start of the line; it is typically treated as 1-based when provided.
  • Values reported by current() are affected by the #line directive in the same way as the predefined macros.

# Member functions

# Creation

# Field access

  • line: returns the represented line number
  • column: returns the represented column number
  • file_name: returns the represented file name
  • function_name: returns the represented function name, if any

# Notes

std::source_location is a vocabulary type for source-coordinate capture. It avoids plumbing __FILE__, __LINE__, and implementation-specific function-name macros through user code while still preserving caller context.

The standard leaves several details implementation-defined, especially the spelling of function names and the meaning of the reported column number. Code should treat these values as diagnostic metadata rather than as a stable interchange format.

# Feature-test macro

MacroValueStdFeature
__cpp_lib_source_location201907LC++20source-code information capture (std::source_location)

# Example

#include <iostream>
#include <source_location>
#include <string_view>
 
void log(const std::string_view message,
         const std::source_location location =
               std::source_location::current())
{
    std::clog << "file: "
              << location.file_name() << '('
              << location.line() << ':'
              << location.column() << ") `"
              << location.function_name() << "`: "
              << message << '\n';
}
 
template<typename T>
void fun(T x)
{
    log(x);
}
 
int main(int, char*[])
{
    log("Hello world!");
    fun("Hello C++20!");
}

# Reference map

AreaPrimary references
Constructionsource_location::source_location, source_location::current
Observersline, column, file_name, function_name

# References

  • C++20 standard (ISO/IEC 14882:2020): 17.8.2 Source location [support.srcloc]
  • C++23 standard (ISO/IEC 14882:2024): 17.8.2 Source location [support.srcloc]

# See also

  • #line: preprocessor control over reported file and line information
  • stacktrace_entry: runtime stack frame metadata for collected stacktraces