c++ - 为什么 C++11 标准中的 INVOKE 工具引用数据成员?

标签 c++ c++11 standards

标准的 $ 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)成员数据指针(通过 bindmem_fn ):

§20.8.10 [func.memfn]

template<class R, class T>
unspecifiedmem_fn(R T::* 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.

如果不能绑定(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

Live example.

关于c++ - 为什么 C++11 标准中的 INVOKE 工具引用数据成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12638393/

相关文章:

xml - 轻量级富文本 XML 格式?

c++ - 二叉树级顺序插入c++

c++ - 带有 OpenGLES2 的 Android NDK 未定义对 glcalls 的引用

c++ - 在单行中通过 XOR 交换整数。在 C++11 中真的允许吗?

C++11 lambdas 到函数指针

c++ - 虚基类初始化

c - 在 C 中将 float +INF、-INF 和 NAN 转换为整数的结果是什么?

google-app-engine - Google App Engine 标准环境还是灵活环境?

c++ - 该代码段中是否会发生死锁,为什么?

c++ - 分配对象数组动态卡住