Why can't operator () of stateless functor be static?

前端 未结 5 1038
我在风中等你
我在风中等你 2020-12-15 19:51

Why is operator () of stateless functor not allowed to be static? Stateless lambda objects are convertible to pointers to free functions having the

5条回答
  •  被撕碎了的回忆
    2020-12-15 20:11

    I can't see any technical reason to forbid a static auto operator()( ... ). But it's a special case, so it would complicate the standard to add support for it. And such complication is not necessary, because it's very easy to emulate:

    struct L
    {
        static void func() {}
    
        void operator()() const { func(); }
    
        operator auto () const
        { 
            return &L::func;
        }
    };
    

    See Johannes' answer for some possibly useful extra info.

提交回复
热议问题