Leonhart Squall
Leonhart Squall

Reputation: 850

Is a pointer-to-function is a function object type?

In my opinion,function object is a class that implements operator().

class Functor
{
public:
    int operator()(int a, int b)
    {
     ...
    }
};

But,in the other question I asked(about std::result_of in c++11),Casey point that a pointer-to-function is a function object type and therefore a callable type.

And in c++ reference:

A function object type is an object type (3.9) that can be the type of the postfix-expression in a function call (5.2.2, 13.3.1.1)

An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not a void type.(3.9)

So it seems that a pointer-to-function is can be treated as a function object,though it turned my opinion of function object.Is that right?

Upvotes: 1

Views: 110

Answers (2)

A function object type is an object type (3.9) that can be the type of the postfix-expression in a function call.

§20.8 [function.objects]

Footnote 230 (N3337), attached to the above sentence in the standard, states:

Such a type is a function pointer or a class type which has a member operator() or a class type which has a conversion to a pointer to function.

Therefore, a function pointer type is a function object type. The standard goes on:

A function object is an object of a function object type.

§20.8 [function.objects]

Therefore a function pointer, being of function object type, is a function object.

Note that, in spite of the traditional connotations of "object", pointers are objects in C++:

An object is a region of storage.

§1.8 [intro.object]

Pointers, occuying a region of storage, are therefore objects.

Upvotes: 4

Brian Bi
Brian Bi

Reputation: 119194

Every pointer type is an object type. This follows from the definition of "object type" quoted in the question details. In particular, function pointer types are object types.

A function pointer type can be the type of the postfix-expression in a function call. For example, if p has type void (*)(), then the expression p() calls the function pointed to by p. So a function pointer type indeed meets the requirements of a function object type.

Upvotes: 2

Related Questions