名稱空間
變體
操作

std::experimental::source_location::function_name

來自 cppreference.com
 
 
實驗性
技術規範
檔案系統庫 (檔案系統 TS)
庫基礎 (庫基礎 TS)
庫基礎 2 (庫基礎 TS v2)
庫基礎 3 (庫基礎 TS v3)
並行性擴充套件 (並行性 TS)
並行性擴充套件 2 (並行性 TS v2)
併發性擴充套件 (併發性 TS)
併發擴充套件 2 (併發 TS v2)
概念 (概念 TS)
範圍 (範圍 TS)
反射 (反射 TS)
數學特殊函式 (特殊函式 TR)
實驗性非 TS
模式匹配
線性代數
std::execution
契約
2D 圖形
 
 
 
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++ 文件,關於 檔名與行資訊