Orient Orient - 3 months ago 24
C++ Question

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

Why is

operator ()
of stateless functor not alllowed to be
? Stateless lambda objects are convertible to pointers to free functions having the same signature as their
operator ()

Stephan T. Lavavej on p. 6 points out that convertion 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
void operator () () const {}
operator auto () const
return &L::operator ();

The error is

overloaded 'operator()' cannot be a static member function

operator ()
is not overloaded.


Per standard 13.5/6,

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.

Additionally, in 13.5.4 it is stated that

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