为什么 C++ 设计者选择不允许非成员运算符 ()()?
Why did the C++ designers choose not to allow non-member operator()()?
我只是在玩 std::function<>
和 operator
s,使 C++ 语句看起来像函数式语言(F#
),发现 [=17] 之间存在差异=] 和 operator<<
。我的代码:
函数 1(运算符重载):
function<int(int)> operator>>(function<int(int)> f1, function<int(int)> f2)
{
function<int(int)> f3 = [=](int x){return f1(f2(x));};
return f3;
}
函数 2(运算符重载):
function<int(int, int)> operator>>(function<int(int, int)> f1, function<int(int)> f2)
{
function<int(int, int)> f3 = [=](int x,int y){return f2(f1(x, y));};
return f3;
}
函数 3(运算符重载):
function<int(int)> operator()(function<int(int, int)> f1, int x)
{
function<int(int)> f2 = [=](int y){return f1(x, y);};
return f2;
}
当函数 1 和函数 2(或运算符重载)时,函数 3 给出错误:
error: ‘std::function<int(int)> operator()(std::function<int(int, int)>, int)’ must be a nonstatic member function
function<int(int)> operator()(function<int(int, int)> f1, int x)
^
为什么operator()
需要是非静态成员?
我认为它不同于 What is the difference between the dot (.) operator and -> in C++? 在那个问题中,答案是用指针来解释的。不过这里我用的是简单的operator()
和operator>>
,跟指针没有关系。
这些是语言设计者决定的规则。 operator()
允许语法看起来像是 class 本身的一部分(std::function
在你的情况下)并且 class 的接口应该由 class本身。
标准在
中对此进行了定义
13.5.4 Function call [over.call]
1 operator()
shall be a non-static member function with an arbitrary number of parameters. [...]
强调我的
对其他运算符做出了类似的决定,例如赋值 =
、下标 []
和 class 成员访问 ->
,而对于像 >>
这样的运算符,他们认为允许在两个(几乎)任意 classes 之间添加运算符是有意义的,而与 class' 接口本身无关。
operator>>()
可以作为非成员函数重载,但不能 operator()
。 operator()
只能是 class
或 struct
.
的非静态成员函数
来自标准:
13.5.4 Function call [over.call]
1 operator()
shall be a non-static member function with an arbitrary number of parameters. It can have default arguments. It implements the function call syntax
postfix-expression ( expression-list opt )
where the postfix-expression evaluates to a class object and the possibly empty expression-list matches the parameter list of an operator()
member function of the class. Thus, a call x(arg1,...)
is interpreted as x.operator()(arg1, ...)
for a class object x
of type T
if T::operator()(T1, T2, T3)
exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3.3).
operator>>()
可以作为非静态成员或作为独立函数调用,具体取决于它是如何为左侧数据类型定义的。语句:
lhs >> rhs
可以解析为:
lhs.operator>>(rhs) // non-static member
或:
operator>>(lhs, rhs) // standalone function
另一方面,operator()
不能作为独立函数调用。它必须在左侧有一些东西才能调用它。语句:
lhs(arguments)
只能解析为:
lhs(arguments) // only if lhs is an actual function
或:
lhs.operator()(arguments) // must be a non-static member
没有允许语句的 C++ 语言语法:
lhs(arguments)
解析为:
operator()(lhs, arguments)
我只是在玩 std::function<>
和 operator
s,使 C++ 语句看起来像函数式语言(F#
),发现 [=17] 之间存在差异=] 和 operator<<
。我的代码:
函数 1(运算符重载):
function<int(int)> operator>>(function<int(int)> f1, function<int(int)> f2)
{
function<int(int)> f3 = [=](int x){return f1(f2(x));};
return f3;
}
函数 2(运算符重载):
function<int(int, int)> operator>>(function<int(int, int)> f1, function<int(int)> f2)
{
function<int(int, int)> f3 = [=](int x,int y){return f2(f1(x, y));};
return f3;
}
函数 3(运算符重载):
function<int(int)> operator()(function<int(int, int)> f1, int x)
{
function<int(int)> f2 = [=](int y){return f1(x, y);};
return f2;
}
当函数 1 和函数 2(或运算符重载)时,函数 3 给出错误:
error: ‘std::function<int(int)> operator()(std::function<int(int, int)>, int)’ must be a nonstatic member function
function<int(int)> operator()(function<int(int, int)> f1, int x)
^
为什么operator()
需要是非静态成员?
我认为它不同于 What is the difference between the dot (.) operator and -> in C++? 在那个问题中,答案是用指针来解释的。不过这里我用的是简单的operator()
和operator>>
,跟指针没有关系。
这些是语言设计者决定的规则。 operator()
允许语法看起来像是 class 本身的一部分(std::function
在你的情况下)并且 class 的接口应该由 class本身。
标准在
中对此进行了定义13.5.4 Function call [over.call]
1
operator()
shall be a non-static member function with an arbitrary number of parameters. [...]
强调我的
对其他运算符做出了类似的决定,例如赋值 =
、下标 []
和 class 成员访问 ->
,而对于像 >>
这样的运算符,他们认为允许在两个(几乎)任意 classes 之间添加运算符是有意义的,而与 class' 接口本身无关。
operator>>()
可以作为非成员函数重载,但不能 operator()
。 operator()
只能是 class
或 struct
.
来自标准:
13.5.4 Function call [over.call]
1
operator()
shall be a non-static member function with an arbitrary number of parameters. It can have default arguments. It implements the function call syntaxpostfix-expression ( expression-list opt )
where the postfix-expression evaluates to a class object and the possibly empty expression-list matches the parameter list of an
operator()
member function of the class. Thus, a callx(arg1,...)
is interpreted asx.operator()(arg1, ...)
for a class objectx
of typeT
ifT::operator()(T1, T2, T3)
exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3.3).
operator>>()
可以作为非静态成员或作为独立函数调用,具体取决于它是如何为左侧数据类型定义的。语句:
lhs >> rhs
可以解析为:
lhs.operator>>(rhs) // non-static member
或:
operator>>(lhs, rhs) // standalone function
另一方面,operator()
不能作为独立函数调用。它必须在左侧有一些东西才能调用它。语句:
lhs(arguments)
只能解析为:
lhs(arguments) // only if lhs is an actual function
或:
lhs.operator()(arguments) // must be a non-static member
没有允许语句的 C++ 语言语法:
lhs(arguments)
解析为:
operator()(lhs, arguments)