std::experi...unction_name(3)
π NAME
std::experimental::source_location::function_name β std::experimental::source_location::function_name (yes bestie, that's the whole name, it's giving "my name is my name" energy π)
okay so real quick, this is from the C++ Standard Libary (not me spelling it wrong, that's literally how the header reads, iykyk π«Ά) and I'm about to make you obsessed with it.
β¨ Synopsis
constexpr const char* function_name() const noexcept;
(library fundamentals TS v2) π
So what does it do? It returns the name of the function associated with the position represented by this object, if any. That's it. That's the whole vibe. Lowkey iconic β¨
π§ Parameters
(none)
literally no arguments, she takes nothing, she needs nothing, main character energy π
πΈ Return value
Here's the tea, two possible outcomes:
- If this object represents a position in a body of a function, it returns an implementation-defined null-terminated byte string corresponding to the name of the function. β¨ Note the "implementation-defined" β so don't come for me if it looks different on your compiler, fr fr.
- Otherwise, an empty string is returned.
no cap, that's the entire contract π¦
π₯ Example
The following example shows how it is possible to use the std::source_location::function_name() to print a name of a function, constructor, destructor, or overloaded operator(). Not me getting emotional about a caller's name π
// Run this code
#include <experimental/source_location>
#include <iostream>
#include <string_view>
inline void function_name(
const std::string_view signature = "()",
const std::experimental::source_location& location
= std::experimental::source_location::current())
{
std::cout
<< location.function_name() // <- name of the caller!
<< signature
<< '\n';
}
void foo() { function_name(); }
struct S {
S() { function_name(); }
S(int) { function_name("(int)"); }
S& operator=(S const&) { function_name("(const S&)"); return *this; }
S& operator=(S&&) { function_name("(S&&)"); return *this; }
~S() { function_name(); }
};
int main()
{
foo();
S p;
S q{42};
p = q;
p = std::move(q);
}
π Possible output
foo()
S()
S(int)
operator=(const S&)
operator=(S&&)
~S()
~S()
look at that. constructor, destructor, overloaded operator=, all just vibing and naming themselves. the destructor showing up twice at the end? she's leaving the group chat, twice. π
π 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)- C++ documentation for Filename and line information
okay babes that's the whole page π source: http://cppreference.com, dated 2024.06.10, filed under std::experi...unction_name(3) β go forth and log your function names, you absolute icon πβπ
π¦ 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