std::source_location::function_name
From cppreference.com
< cpp | utility | source location
| constexpr const char* function_name() const noexcept; |
(C++20以降) | |
このオブジェクトが表す位置に関連付けられた関数の名前を返します。存在する場合。
目次 |
[編集] パラメータ
(なし)
[編集] 戻り値
このオブジェクトが関数の本体内の位置を表す場合、関数の名前に対応する、実装定義のヌル終端バイト文字列を返します。
それ以外の場合は、空文字列が返されます。
[編集] 例
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()
[編集] 関連項目
| このオブジェクトが表す行番号を返す (公開メンバ関数) | |
| このオブジェクトが表す列番号を返す (公開メンバ関数) | |
| このオブジェクトが表すファイル名を返す (公開メンバ関数) |