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 (i.e.):
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.
CodePudding user response:
[expr.ref]:
[for the expression
E1.E2
]....ifE1.E2
refers to a non-static member function...The expression can be used only as the left-hand operand of a member function call.