为什么C ++ 11标准中的INVOKE工具引用数据成员? [英] Why does INVOKE facility in the C++11 standard refer to data members?
问题描述
$ 20.8.2的标准描述了INVOKE工具,它主要用于描述如何使用可变参数列表在整个标准库中调用可调用对象:
$ 20.8.2 of the standard describes the INVOKE facility that is mostly used to describe how callables are called with variadic argument lists throughout the standard library:
定义INVOKE(f,t1,t2,...,tN)如下:
Define INVOKE (f, t1, t2, ..., tN) as follows:
- )(t 2,...,t N)
当f是指向类T的成员函数的指针并且t1是
类型T的对象或对类型的对象的引用T或对从T导出的类型的对象的引用;
— (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)。 。,tN)
当f是指向类T的成员函数的指针时,并且t1不是
中的一个在前一项中描述的类型;
— ((*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
或对类型T的对象的
引用或对从T派生的类型的对象的引用;
— 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
当N == 1且f是指向类T的成员数据的指针并且t1不是前面项目中描述的类型
中的一个时;
— (*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)
。
第三个和第四个项目是什么?据我所知,即使 f
是可调用的,它们也不调用 f
。他们的用户情况是什么。也许这是标准中的拼写错误, * f()
是为了?
What are the third and the fourth item for? As far as I can tell, they don't call f
even if f
is callable. What's the user case for them. Maybe it's a typo in the standard and *f()
was intended?
推荐答案
INVOKE
是这样指定的,因为您可以实际绑定成员数据指针(通过 bind
和 mem_fn
):
INVOKE
is specified like that because you can actually bind member data pointers (through bind
and mem_fn
):
§20.8.10[func.memfn]
模板< class R,class T>
unspecified
mem_fn(RT :: * pm);
p1 返回:一个简单的调用包装器(20.8.1) fn
fn(t,a2,...,aN)等效于 INVOKE(pm,t,a2,...,aN)
20.8.2)。 fn
应具有嵌套类型 result_type
,它是 pm
当 pm
是指向成员函数的指针。
p1 Returns: A simple call wrapper (20.8.1) fn
such that the expression fn(t, a2, ..., aN)
is equivalent to INVOKE(pm, t, a2, ..., aN)
(20.8.2). fn
shall have a nested type result_type
that is a synonym for the return type of pm
when pm
is a pointer to member function.
如果你不能绑定成员数据指针,我不认为特殊的字眼会存在。
I don't think that special wording would exist if you couldn't bind member data pointers.
#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 ++ 11标准中的INVOKE工具引用数据成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!