std::experimental::source_location
From cppreference.com
< cpp | experimental
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Experimental
Filesystem library (filesystem TS)
Library fundamentals (library fundamentals TS)
Library fundamentals 2 (library fundamentals TS v2)
Library fundamentals 3 (library fundamentals TS v3)
Extensions for parallelism (parallelism TS)
Extensions for parallelism 2 (parallelism TS v2)
Extensions for concurrency (concurrency TS)
Extensions for concurrency 2 (concurrency TS v2)
Concepts (concepts TS)
Ranges (ranges TS)
Reflection (reflection TS)
Mathematical special functions (special functions TR)
Library fundamentals v2
experimental::source_location
std::experimental::source_location
Merged into ISO C++ The functionality described on this page was merged into the mainline ISO C++ standard as of 7/2019, see std::source_location (since C++20)
Defined in header
<experimental/source_location>
struct source_location;
(library fundamentals TS v2)
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.
Contents
[edit] Member functions
Creation
Other special member functions
operator=
(implicitly declared)
(public member function) [edit]
Field access
[edit] Example
Run this code
#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!"); }
Possible output:
info:main.cpp:15 Hello world!