c++ - 显示名称函数模板名称
问题描述
我有一个正在尝试处理的函数包装类。
我希望能够显示传递给模板的函数的名称。
template<auto Func>
struct FuncWrapper final {
FuncWrapper() {
StrFuncName = typeid(Func).name();
std::cout << StrFuncName << std::endl;
}
template<typename... Args>
auto operator()(Args&&... args) const { }
std::string StrFuncName;
};
void Function() { }
FuncWrapper<Function> wrapper;
解决方案
这在标准 C++ 中是不可能的。拆解 typeid 也无济于事,因为您只会获得函数类型的名称,而不是您实际赋予函数的名称。
您可以获得的最接近的是预定义__func__
常量,但它只存在于您想要获取名称的函数范围内。
void foo()
{
std::cout << __func__; // will print "foo"
}
推荐阅读
- c# - 温莎城堡注册意外行为
- php - 我的 API 应该使用什么样的 DigitalOcean 液滴。它使用php和mysql,涉及所有mysql操作
- reactjs - React Native-设置View的高度与内容相同
- vpn - 如何使用 VPN 连接在远程 postegresql 服务器上连接 pgAdmin?
- c# - 通过.Net Core中的依赖注入映射接口以使用加载的配置来连接类
- reactjs - 通过状态不工作通过 img src 更改图像
- sql - 如何优化 Oracle 中不断更新特定行的更新 SQL
- vba - 修改代码以将范围复制为图片
- python - Python正则表达式:仅当模式重复n次时才匹配
- node.js - Redux createStore,compose 函数 - 这段代码是如何工作的?