Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

C++ std::source_location class

Reference for std::source_location (C++20): capture the file, line, column and function name of a call site as a default argument, for logging and asserts.

std::source_location (header <source_location>, C++20) is a small value type that describes a place in the source code: file name, line, column and enclosing function. It replaces the old __FILE__, __LINE__ and __func__ macros with something you can pass around and capture in a function signature.

The key trick is the default argument. source_location::current() evaluated as a default argument captures the location of the caller, not of the function that declares the parameter. That makes logging helpers and custom assertions report the line where they were used, without a macro.

C++
void log(std::string_view msg,
         std::source_location where = std::source_location::current());

Try it

C++ Logging with the caller's line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Fields of a location

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
struct source_location {
    static consteval source_location current() noexcept;
    constexpr uint_least32_t line() const noexcept;
    constexpr uint_least32_t column() const noexcept;
    constexpr const char* file_name() const noexcept;
    constexpr const char* function_name() const noexcept;
};
Member Description
current() Returns the location where it is called, or the caller's location when used as a default argument.
line() 1-based line number.
column() Implementation-defined column, often 1-based. GCC reports the column of the expression.
file_name() The path as given to the compiler. Not normalized.
function_name() Implementation-defined text. GCC prints a readable signature such as void Widget::draw() for member functions.

Returns: a trivially copyable, default-constructible object. A default-constructed one has line 0, column 0 and empty strings.

Errors: none.

Usage notes

  • Default arguments only. Writing current() inside a function body reports that line of the body. To record the caller, put it in the parameter list.
  • Templates and variadics: a trailing default parameter cannot follow a parameter pack. Wrap the arguments in a struct whose constructor takes the location, or place the location first.
  • Output depends on the build: the file path changes between machines and -ffile-prefix-map, and line numbers change when the file is edited. Never compare against fixed strings in tests.
  • The strings are static storage and valid for the whole program, so storing the pointers is safe.
  • Cost is zero at run time. The values are constants emitted by the compiler.
  • Macros remain for the preprocessor. __LINE__ is still the right tool inside #line-style or token-pasting tricks.
  • Contrast with stack traces: source_location names one place; a stack trace names the whole call chain (see std::stacktrace).