std::source_location::function_name
來自 cppreference.com
< cpp | 工具庫 | 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()
[編輯] 另請參閱
返回此物件表示的行號 (公共成員函式) | |
返回此物件表示的列號 (公共成員函式) | |
返回此物件表示的檔名 (公共成員函式) |