标准的 $ 20.8.2 描述了 INVOKE 工具,该工具主要用于描述如何在整个标准库中使用可变参数列表调用可调用对象:
Define INVOKE (f, t1, t2, ..., tN) as follows:
—
(t1.*f)(t2, ..., tN)
when f is a pointer to a member function of a class T and t1 is an object of type T or a reference to an object of type T or a reference to an object of a type derived from T;—
((*t1).*f)(t2, ..., tN)
when f is a pointer to a member function of a class T and t1 is not one of the types described in the previous item;—
t1.*f
when N == 1 and f is a pointer to member data of a class T and t1 is an object of type T or a reference to an object of type T or a reference to an object of a type derived from T;—
(*t1).*f
when N == 1 and f is a pointer to member data of a class T and t1 is not one of the types described in the previous item;—
f(t1, t2, ..., tN)
in all other cases.
第三项和第四项是做什么用的?据我所知,即使 f
是可调用的,他们也不会调用 f
。他们的用户案例是什么。也许这是标准中的错字,而 *f()
是有意的?
最佳答案
INVOKE
之所以这样指定是因为您实际上可以绑定(bind)成员数据指针(通过 bind
和 mem_fn
):
§20.8.10 [func.memfn]
template<class R, class T>
unspecified
mem_fn(R T::* pm);
p1 Returns: A simple call wrapper (20.8.1)
fn
such that the expressionfn(t, a2, ..., aN)
is equivalent toINVOKE(pm, t, a2, ..., aN)
(20.8.2).fn
shall have a nested typeresult_type
that is a synonym for the return type ofpm
whenpm
is a pointer to member function.
如果不能绑定(bind)成员数据指针,我认为不会存在特殊的措辞。
#include <functional>
#include <iostream>
struct X{
int n = 5;
};
int main(){
X x;
auto f = std::mem_fn(&X::n);
std::cout << f(&x) << "\n";
}
输出:5
关于c++ - 为什么 C++11 标准中的 INVOKE 工具引用数据成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12638393/