std::experimental::source_location::function_name
來自 cppreference.com
< cpp | experimental | source_location
constexpr const char* function_name() const noexcept; |
(庫基礎 TS v2) | |
返回與此物件所表示位置關聯的函式名稱(如果有)。
目錄 |
[編輯] 引數
(無)
[編輯] 返回值
如果此物件表示函式體中的某個位置,則返回一個實現定義的以 null 結尾的位元組字串,對應於該函式的名稱。
否則,返回一個空字串。
[編輯] 示例
以下示例展示瞭如何使用 std::source_location::function_name()
來列印函式、建構函式、解構函式或過載 operator()
的名稱。
執行此程式碼
#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); }
可能的輸出
foo() S() S(int) operator=(const S&) operator=(S&&) ~S() ~S()
[編輯] 參閱
返回此物件表示的行號 (公共成員函式) | |
返回此物件表示的列號 (公共成員函式) | |
返回此物件表示的檔名 (公共成員函式) | |
C++ 文件,關於 檔名與行資訊
|