且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

指向类模板成员的函数指针

更新时间:2022-02-11 08:57:16

没有.指向成员的指针必须指向某物.类模板中的成员函数不是单个函数,而是一系列函数-每个函数都有不同的类型.甚至可能存在T,而list<T>::lessThan并不存在,或者是类型还是变量!

No there isn't. A pointer-to-member has to point to something. A member function in a class template isn't a single function, it's a family of functions - and each one has a different type. There could even be a T for which list<T>::lessThan doesn't exist, or is a type or a variable!

如果我们为这样一个指向成员的指针做了别名:

If we made an alias for one such pointer to member:

template <typename T>
using CompFun = bool (list<T>::*)(T, T);

然后很明显CompFun<int>CompFun<string>是不同的类型.您不能创建通用的CompFun<T>变量.

Then it's obvious that CompFun<int> and CompFun<string> are different types. You cannot create a generic CompFun<T> variable.

但是,根据您要尝试执行的操作,可能有一种很好的方法来完成该操作.

Depending on what it is you're trying to do, though, there might be a good way to accomplish that.