Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are function pointers function objects in C++?

The C++ standard defines function objects as:

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

First I was thinking that function objects were functors, but then I realized that for a function pointer ptr of type P (not a function, but a function pointer), std::is_object_v<P> is true and can be called with the ptr(Args...) syntax.

I am right that function pointers are considered as function objects by the standard? And if they are not what part of the definition is not satisfied by function pointers?

like image 848
Vincent Avatar asked Mar 27 '18 01:03

Vincent


People also ask

Are function pointers in C?

In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.

Can a pointer be an object?

A pointer is a type of variable that carries location information. In this case, the example variable will store the address of an Order object that we want to interact with. We initialize the pointer variable by using the C++ new operator to construct a new object of type Order.

What is a pointer object in C?

In C++, a pointer holds the address of an object stored in memory. The pointer then simply “points” to the object. The type of the object must correspond with the type of the pointer. type *name; // points to a value of the specified type.

Is a pointer a function?

A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions.


2 Answers

Function pointer is what it sounds like: a pointer to function. As itself it's a storage containing a pointer object, which returns a callable of function type.

If you take time and read first chapters of standard, you 'll understand that any variable declaration declares some type of storage that contains objects. Those can be objects of primitive types or classes. Essentially in C++ anything that can be stored is object.

By declaring function pointer you create storage that can store address of that function and operator() can be used

Anther type of callable closure can be created by lambda expression. They are not function objects, each expression creates a unique callable object, but captureless lambdas can be used as one, e.g. to assign it to a function pointer, e.g.

double (*square)(double) = [](double a)->double { return a*a; };

after this you can call it using expression like square(3.6);

For functions and lambda call operator operator() is supplied by language, by defining operator() for a class you create what people often call "functor", which is misnomer because actual functors in mathematics or such languages like Haskell do not store state. Result of lambda expression is a "functor" created by compiler, which stores states of captured objects.

Naming those objects callable might be a little misleading too, because as a concept in C++, a callable object is any object that can be used with INVOKE operation, which include pointers to data members even while no function calls happen.

it leaves only one option, if we can use function call with said object, it's a function object. It can be function, lambda expression, function object, function pointer, member function pointer with specified class instance ( obj.*memberptr or objptr->*memberptr - call of member function is very special) - they are function objects.

like image 73
Swift - Friday Pie Avatar answered Sep 24 '22 15:09

Swift - Friday Pie


Yes, they are. The term "object" in C++ standard does not mean "object" in the OOP sense. An int is an object.

like image 31
T.C. Avatar answered Sep 24 '22 15:09

T.C.