std::source...ion::current(3)
std::source...ion::current(3) π
okay bestie, quick vibe check: this page is from the C++ Standard Libary manual (yes, that's exactly how the header spells it, no cap), and the page title is std::source...ion::current(3). It's giving "where am I actually being called from?" energy, and honestly it ate. π¦
β¨ NAME β¨
std::source_location::currentβstd::source_location::current
Like... the name is the name, and the one-line description is also the name. Iconic, lowkey circular, we move. π
πΈ Synopsis πΈ
static consteval source_location current() noexcept;
(since C++20)
It constructs a new source_location object corresponding to the location of the call site. periodt. β¨
π Parameters π
(none) β no parameters, bestie. zero. nada. π«Ά
π Return value π
- If
current()is invoked directly (via a function call that namescurrent()), it returns asource_locationobject with implementation-defined values representing the location of the call. Those values should be affected by the#linepreprocessor directive in the same manner as the predefined macros__LINE__and__FILE__. - If
current()is used in a default member initializer, the return value corresponds to the location of the constructor definition or aggregate initialization that initializes the data member. - If
current()is used in a default argument, the return value corresponds to the location of the call tocurrent()at the call site. - If
current()is invoked in any other manner, the return value is unspecified. π
ngl "unspecified" is the "you're on your own, bestie" of the C++ standard. not mean, just honest. π
π₯ Notes π₯
std::source_location::current typically requires compiler's built-in implementation. like, the compiler has to be in on the bit. π§
π Example π
// Run this code
#include <source_location>
#include <iostream>
struct src_rec {
std::source_location srcl = std::source_location::current();
int dummy = 0;
src_rec(std::source_location loc = std::source_location::current())
:
srcl(loc) // values of member refer to the location of the
calling function
{}
src_rec(int i) : // values of member refer to this location
dummy(i)
{}
src_rec(double) // values of member refer to this location
{}
};
std::source_location src_clone(std::source_location a =
std::source_location::current())
{
return a;
}
std::source_location src_make()
{
std::source_location b = std::source_location::current();
return b;
}
int main()
{
src_rec srec0;
src_rec srec1(0);
src_rec srec2(0.0);
auto s0 = std::source_location::current();
auto s1 = src_clone(s0);
auto s2 = src_clone();
auto s3 = src_make();
std::cout
<< srec0.srcl.line() << ' ' << srec0.srcl.function_name() <<
'\n'
<< srec1.srcl.line() << ' ' << srec1.srcl.function_name() <<
'\n'
<< srec2.srcl.line() << ' ' << srec2.srcl.function_name() <<
'\n'
<< s0.line() << ' ' << s0.function_name() << '\n'
<< s1.line() << ' ' << s1.function_name() << '\n'
<< s2.line() << ' ' << s2.function_name() << '\n'
<< s3.line() << ' ' << s3.function_name() << '\n';
}
Possible output:
31 int main()
12 src_rec::src_rec(int)
15 src_rec::src_rec(double)
34 int main()
34 int main()
36 int main()
25 std::source_location src_make()
π See also π
constructorβ constructs a newsource_locationwith implementation-defined values (public member function)current[static]β obtains the current stacktrace or its given part (public static member function ofstd::basic_stacktrace<Allocator>)
okay that's the whole page, bestie β source: http://cppreference.com, version/date: 2024.06.10, page: std::source...ion::current(3). bye, go get that source location π
β¨
π¦ 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