Why does an INVOKE object in C ++ 11 apply to data members?

$ 20.8.2 of the standard describes the INVOKE tool, which is mainly used to describe how invoked calls are called with variable argument lists in the standard library:

Define INVOKE (f, t1, t2, ..., tN) as follows:

- (t1.*f)(t2, ..., tN)when f is a pointer to a member function of the 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 type derived from T;

- ((*t1).*f)(t2, ..., tN)when f is a pointer to a member function of the class T and t1 is not one of the types described in the previous paragraph;

- t1.*fwhen N == 1 and f is a pointer to the data of an element of 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 type obtained from T;

- (*t1).*fwhen N == 1 and f is a pointer to the data of an element of class T and t1 is not one of the types described in the previous paragraph;

- f(t1, t2, ..., tN)in all other cases.

Why do we need the third and fourth elements? As far as I can tell, they do not call f, even if it fis called. What is for them for users. Perhaps it was a standard sample version and *f()?

+3
source share
1 answer

INVOKEis specified like this because you can actually bind the item's data pointers (via bindand mem_fn):

ยง20.8.10 [func.memfn]

template<class R, class T>
unspecified mem_fn(R T::* pm);

p1 : (20.8.1) fn , fn(t, a2, ..., aN) INVOKE(pm, t, a2, ..., aN) (20.8.2). fn result_type, pm , pm -.

, , .

#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

.

+7

All Articles