Section

std::experimental::source_location

The source_location class represents certain information about the source code, such as file names, line numbers, and function names. Previously, functions that desire to obtain this information about the call site (for logging, testing, or debugging purposes) must use macros so that predefined macros like LINE and FILE are expanded in the context of the caller. The source_location class provides a better alternative.

# Declarations

struct source_location;

(library fundamentals TS v2)

# Example

#include <experimental/source_location>
#include <iostream>
#include <string_view>
 
void log(const std::string_view message,
         const std::experimental::source_location location =
               std::experimental::source_location::current())
{
    std::cout << "info:"
              << location.file_name() << ':'
              << location.line() << ' '
              << message << '\n';
}
 
int main()
{
    log("Hello world!");
}