为什么无状态函子的operator()不能是静态的?
为什么不允许无状态函子的操作符()
保持静态
?无状态lambda对象可转换为指向具有与其 operator()
相同签名的自由函数的指针。
Why is operator ()
of stateless functor not allowed to be static
? Stateless lambda objects are convertible to pointers to free functions having the same signature as their operator ()
.
Stephan T. Lavavej 。 6指出,转换为函数指针只是操作符FunctionPointer()
(引用)。但是对于非成员函数,我无法获得指向 operator()
的相应指针。对于函子 struct F {void运算符()(){}}
,似乎无法转换& F :: operator()
到类型为的实例,使用P = void(*)();
。
Stephan T. Lavavej on p. 6 points out that conversion to a function pointer is just an operator FunctionPointer()
(cite). But I can't obtain a corresponding pointer to operator ()
as to non-member function. For functor struct F { void operator () () {} }
it seems to be impossible to convert &F::operator ()
to instance of type using P = void (*)();
.
代码:
struct L
{
static
void operator () () const {}
operator auto () const
{
return &L::operator ();
}
};
错误是
重载的'operator()'不能是静态成员函数
overloaded 'operator()' cannot be a static member function
但 operator()
不超载。
每个标准13.5 / 6,
Per standard 13.5/6,
操作符函数应为非静态成员函数或为非成员函数,并至少具有
个参数, type是一个类,对一个类的引用,一个枚举或对
枚举的引用。
An operator function shall either be a non-static member function or be a non-member function and have at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.
在13.5.4中声明
operator()
应该是具有任意数量的非静态成员函数参数。它可以具有
个默认参数。它实现了函数调用语法
后缀表达式
(
expression-list
opt
)
其中
后缀表达式
计算一个类对象,可能为空的
表达式列表
匹配
该类成员函数
operator()
的参数列表。因此,对于类对象的调用
x(arg1,...)
被解释为
x.operator()(arg1,...)
x
类型
T
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