c++ - 非静态成员函数模板地址
问题描述
我试图了解是否可以获取非静态成员函数模板的地址。以下不起作用,使用&sz<int>
会导致其他错误。获取非静态成员函数模板地址的正确方法是什么?
311 struct XYZ
312 {
313 template <typename Z>
314 void sz()
315 {
316 }
317
318 void func()
319 {
320 auto z = sz<int>;
321 }
322 };
导致错误
vs.cc:320:16: error: reference to non-static member function must be called; did you mean to call it with no arguments?
auto z = sz<int>;
解决方案
void sz()
模板无关紧要,因为sz<int>
它是成员函数。C++ 中没有“地址”的概念——这是一个实现细节。你可以拥有一个指向成员函数的指针,它的语法是:
auto z = &XYZ::sz<int>;
要在内部调用它func()
,您需要以下语法:
(this->*z)();
推荐阅读
- css - 如何在ionic4的类中调用变量?
- c# - 获取 dll 的路径 - 原始路径,而不是阴影程序集
- java - java cmd进程中如何按顺序执行多个命令
- java - 带有流弹簧引导的 DTO 映射
- java - 如何让处理在文本文件中获取数组的一项并对其做出决定
- image - 取消选择时停止 Gimp 合并图层
- angular-calendar - 角日历 | 需要一个默认的滚动视图到 8AM-11PM 在日/周中具有固定的标题
- django - 如何通过按下按钮更新 Django 中的页面
- oracle - 无法在 Oracle SQL Developer v19.0.1 中创建 PRIVATE TEMPORARY TABLE(错误:ORA-00905)
- go - 我应该在哪里存储服务注册服务器的端点?