命名空间
变体
操作

std::source_location::function_name

来自 cppreference.cn
 
 
 
 
constexpr const char* function_name() const noexcept;
(since C++20)

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

目录

[edit] 参数

(无)

[edit] 返回值

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

否则,返回一个空字符串。

[edit] 示例

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()

[edit] 参见

返回此对象表示的行号
(公共成员函数) [编辑]
返回此对象表示的列号
(公共成员函数) [编辑]
返回此对象表示的文件名
(公共成员函数) [编辑]