std::source_location::function_name
来自cppreference.com
<tbody>
</tbody>
constexpr const char* function_name() const noexcept; |
(C++20 起) | |
返回与此对象所表示的位置关联的函数名,若存在。
参数
(无)
返回值
若此对象表示函数体内的位置,则返回对应于函数的名字的由实现定义的空终止字节字符串。
否则,返回空字符串。
示例
以下示例展示能如何用 std::source_location::function_name() 打印函数、构造函数、析构函数或重载的 operator() 的名字。
运行此代码
#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()); // 打印调用方的名字
}
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()
参阅
| 返回此对象表示的行号 (公开成员函数) | |
| 返回此对象表示的列号 (公开成员函数) | |
| 返回此对象表示的文件名 (公开成员函数) |