std::source_location(3)
okay bestie, welcome to the C++ Standard Libary manual page for std::source_location(3) π
β¨ this one is giving call-site-info realness, and yes we're keeping every detail.
πΈ NAME πΈ
std::source_location β std::source_location. that's the name, no notes, periodt π
β¨ Synopsis β¨
Defined in header <source_location>
struct source_location; (since C++20)
okay so the std::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 std::source_location class provides a better alternative.
std::source_location meets the DefaultConstructible, CopyConstructible, CopyAssignable and Destructible requirements. Lvalue of std::source_location meets the Swappable requirement.
Additionally, the following conditions are true:
std::is_nothrow_move_constructible_v<std::source_location>,std::is_nothrow_move_assignable_v<std::source_location>, andstd::is_nothrow_swappable_v<std::source_location>.
It is intended that std::source_location has a small size and can be copied efficiently. iconic. π¦
β οΈ unspecified tea, bestie: it is unspecified whether the copy/move constructors and the copy/move assignment operators ofstd::source_locationare trivial and/orconstexpr. so don't go assuming, fr fr π
π οΈ Member functions π οΈ
Creation
constructorβ constructs a newsource_locationwith implementation-defined values (public member function).current[static]β constructs a newsource_locationcorresponding to the location of the call site (public static member function).
Field access
lineβ return the line number represented by this object (public member function).columnβ return the column number represented by this object (public member function).file_nameβ return the file name represented by this object (public member function).function_nameβ return the name of the function represented by this object, if any (public member function).
π Notes π
Feature-test macro
- Feature-test macro:
__cpp_lib_source_location- Value:
201907L - Std:
(C++20) - Feature: Source-code information capture (
std::source_location)
- Value:
π§ͺ Example π§ͺ
// Run this code
#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); // line 20
}
int main(int, char*[])
{
log("Hello world!"); // line 25
fun("Hello C++20!");
}
Possible output:
file: main.cpp(25:8) `int main(int, char**)`: Hello world!
file: main.cpp(20:8) `void fun(T) [with T = const char*]`: Hello C++20!
π See also π
#lineβ changes the source code's line number and, optionally, the current file name (preprocessing directive).stacktrace_entry(C++23) β representation of an evaluation in a stacktrace (class).
that's the page, bestie π«Ά footer vibes: source/version http://cppreference.com Β· date 2024.06.10 Β· page std::source_location(3). mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms