简体   繁体   English

非 static 成员 function 模板的地址

[英]address of non static member function template

I'm trying to understand if it's possible to take the address of a non-static member function template.我试图了解是否可以获取非静态成员 function 模板的地址。 Below does not work and using &sz<int> results in other errors.下面不起作用,使用&sz<int>会导致其他错误。 What is the right way to get the address of non static member function template?获取非 static 成员 function 模板地址的正确方法是什么?

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 };

results in an error导致错误

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>;

It does not matter that void sz() is a template because sz<int> is a member function. void sz()是模板并不重要,因为sz<int>是成员 function。 There is no notion of "address" in C++ - this is an implementation detail. C++ 中没有“地址”的概念——这是一个实现细节。 What you can have is a pointer to member function , and the syntax for it is:你可以拥有一个指向成员 function的指针,它的语法是:

auto z = &XYZ::sz<int>;

To call it inside func() , you need the following syntax:要在func()中调用它,您需要以下语法:

(this->*z)();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM