绑定成员函数的指针/引用的禁止语法

Kyl*_*fel 2 c++ language-lawyer

假设我有以下内容:

struct A {
  int foo(int bar) const { return bar; }
};
Run Code Online (Sandbox Code Playgroud)

我想指定一个引用“绑定”成员函数的名称(即):

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);
Run Code Online (Sandbox Code Playgroud)

然后调用绑定的成员函数就很简单:

assert(opt1(42) == 42); // If 'opt1' were allowed
assert(opt2(42) == 42); 
assert(opt3(42) == 42);
Run Code Online (Sandbox Code Playgroud)

在我看来,opt1这将是实现目标的首选解决方案。opt1但是,该语言禁止通过 via 指定绑定函数。

我的问题纯粹是合法的:C++(20) 标准的哪一部分禁止像这样的构造opt1 我的问题不是为什么,而是在哪里

Sne*_*tel 5

[expr.ref]:

[对于表达式E1.E2]....ifE1.E2引用非静态成员函数...该表达式只能用作成员函数调用的左侧操作数。