Namespaces
Variants

std::experimental::source_location:: function_name

From cppreference.net
constexpr const char * function_name ( ) const noexcept ;
(库基础技术规范 v2)

返回与此对象所表示位置相关联的函数名称(如果存在)。

目录

参数

(无)

返回值

如果此对象表示函数体中的某个位置,则返回一个实现定义的以空字符结尾的字节字符串,该字符串对应于函数的名称。

否则,将返回空字符串。

示例

以下示例展示了如何使用 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() // <- 调用者名称!
        << 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++ documentation for 文件名和行信息