I am reading C Primer 5th edition and there I came across the following statement:
As a result, unlike ordinary function pointers, a pointer to a member is not a callable object; these pointers do not support the function-call operator.
So my question is: Is the highlighted part correct according to the standard?
My current understanding and intuition are that functionally they behave in the same manner, so a pointer to a member function should also be a callable object(as its name suggest).
Note that I am not asking whether the book is correct in saying that member function pointers do not support the function call operator. Because I already know that that part of the statement is correct. What I am asking is whether the pointer to the member function is a callable object according to the standard.
CodePudding user response:
What i am asking is that whether pointer to member function are callable object according to the standard.
Yes, a pointer to a member function is a callable object according to the standard.
From func.def#4:
A callable object is an object of a callable type.
And from func.def#3:
A callable type is a function object type ([function.objects]) or a pointer to member.
Thus, the highlighted part of the quoted statement that says "a pointer to member is not a callable object" from C Primer is incorrect according the standard.
CodePudding user response:
As a result, unlike ordinary function pointers, a pointer to member is not a callable object; these pointers do not support the function-call operator.
So my question is, if the above quoted statement(the highlighted part in particular) correct according to the standard?
To my surprise, the standard actually says in [func.def]/3
- The following definitions apply to this Clause:
- ...
- A callable type is a function object type ([function.objects]) or a pointer to member.
- A callable object is an object of a callable type.
- A call wrapper type is a type that holds a callable object and supports a call operation that forwards to that object.
- A call wrapper is an object of a call wrapper type.
- A target object is the callable object held by a call wrapper.
So in fact a pointer to member function is a callable object according to the standard ... in the context of describing which types can be the target of the function objects defined in <functional>
.
There is no alternative definition of a "callable object" in the standard that would correspond to the more general sense implied by the book.
Summary:
The only standard definition of a callable object is when defining the types that are valid targets for the function objects in
<functional>
.A pointer to member function is a valid target, and is callable so long as the instance pointer is either bound or passed to the function call
The customary definition of a callable object as one providing a function call operator is not directly supported by the standard.
This definition is arguably more useful though, because it tells me how I can use the object.