std::source...unction_name(3)
π NAME π
std::source_location::function_name β std::source_location::function_name π«Ά
Okay so this is the std::source...unction_name(3) page from the C++ Standard Library, and itβs all about std::source_location::function_name. Itβs giving βbestie, what function are we even in?β energy. β¨
β¨ Synopsis β¨
constexpr const char* function_name() const noexcept; (since C++20)
This one is available since C++20. It returns the name of the function associated with the position represented by this object β if there even is one. No cap. π
π«Ά Parameters π«Ά
(none) β literally none. No arguments, no sneaky extras. π
π Return value π
If this object represents a position in a body of a function, you get an implementation-defined null-terminated byte string corresponding to the name of the function. Otherwise? An empty string is returned. Thatβs the whole vibe check. π¦
π§ Example π§
std::source_location::function_name may help to obtain the names of functions β including the special functions β alongside with their signatures. Obsessed. π
Run this code, bestie:
// Run this code
#include <cstdio>
#include <utility>
#include <source_location>
inline void print_function_name(
const std::source_location& location = std::source_location::current())
{
std::puts(location.function_name()); // prints the name of the caller
}
void foo(double &&) { print_function_name(); }
namespace bar { void baz() { print_function_name(); } }
template <typename T> auto pub(T) { print_function_name(); return 42; }
struct S {
S() { print_function_name(); }
S(int) { print_function_name(); }
~S() { print_function_name(); }
S& operator=(S const&) { print_function_name(); return *this; }
S& operator=(S&&) { print_function_name(); return *this; }
};
int main(int, char const* const[])
{
print_function_name();
foo(3.14);
bar::baz();
pub(0xFULL);
S p;
S q{42};
p = q;
p = std::move(q);
[] { print_function_name(); }();
}
π Possible output π
int main(int, const char* const*)
void foo(double&&)
void bar::baz()
auto pub(T) [with T = long long unsigned int]
S::S()
S::S(int)
S& S::operator=(const S&)
S& S::operator=(S&&)
main(int, const char* const*)::<lambda()>
S::~S()
S::~S()
π See also π
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)
Mwah π β source: http://cppreference.com, version/date: 2024.06.10, page: std::source...unction_name(3). Byeee bestie π«Ά
π¦ 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