19.4.3. Using Member Functions as Callable Objects
As we’ve seen, to make a call through a pointer to member function, we must use the .*
or ->*
operators to bind the pointer to a specific object. 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 (§ 10.3.2, p. 388).
Because a pointer to member is not a callable object, we cannot directly pass a pointer to a member function to an algorithm. As an example, if we wanted to find the first empty string
in a vector
of string
s, the obvious call won’t work:
auto fp = &string::empty; // fp points to the string empty function// error: must use .* or ->* to call a pointer to memberfind_if(svec.begin(), ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.