Namespaces
Variants

std::source_location:: function_name

From cppreference.net
Utilities library
constexpr const char * function_name ( ) const noexcept ;
(C++20以降)

このオブジェクトが表す位置に関連付けられた関数の名前があれば、それを返します。

目次

パラメータ

(なし)

戻り値

このオブジェクトが関数本体の位置を表す場合、その関数名に対応する実装定義のNULL終端バイト文字列を返します。

それ以外の場合、空の文字列が返されます。

std::source_location::function_name は、関数名(特殊関数を含む)とそのシグネチャを取得するのに役立ちます。

#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(); }();
}

出力例:

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()

関連項目

このオブジェクトが表す行番号を返す
(public member function)
このオブジェクトが表す列番号を返す
(public member function)
このオブジェクトが表すファイル名を返す
(public member function)