为什么无状态函子的operator()不能是静态的? [英] Why can't operator () of stateless functor be static?

查看:107
本文介绍了为什么无状态函子的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

这篇关于为什么无状态函子的operator()不能是静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆