简体   繁体   中英

Forbidden syntax for pointer/reference to bound member function

Suppose I have the following:

struct A {
  int foo(int bar) const { return bar; }
};

and I want to specify a name that refers to a "bound" member function (ie):

A a;
auto opt1 = a.foo; // Forbidden, instead do something like...
auto opt2 = [&a] (int i) { return a.foo(i); }; // or ...
auto opt3 = std::bind(&A::foo, a, std::placeholders::_1);

it is then simple to invoke the bound member function:

assert(opt1(42) == 42); // If 'opt1' were allowed
assert(opt2(42) == 42); 
assert(opt3(42) == 42);

In my view, opt1 would be the preferred solution to achieve the goal. However, specifying a bound function via opt1 is forbidden by the language.

My question is purely legal: What part of the C++(20) standard forbids a construct like opt1 ? My question is not why , but where .

[expr.ref]:

[for the expression E1.E2 ]....if E1.E2 refers to a non-static member function...The expression can be used only as the left-hand operand of a member function call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM