std::experi...unction_name(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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