It fails to compile because
ip isn’t a compile-time constant.
All template arguments must be compile-time constants.
In this case, to trigger the correct template you can
as I will demonstrate in a moment…
This is either a trick question or the author doesn’t know what
(I’m going to assume it’s a trick question designed to test your knowledge,
or, since I’m the one providing the answer, my knowledge.
There’s no such thing as a pointer to a static member function because a static member function behaves like a normal function,
so to point to a static member function you’d use a normal function pointer.
Similarly a pointer to a static class member would just be a normal pointer.
Pointers to non-static member functions are however separate entities with special syntax, as I will now demonstrate:
std::cout << i << '\n';
template<int * pointer>
std::cout << pointer << '\n';
static int static_member_function()
static int static_member_variable;
int some_class::static_member_variable = 0;
template<typename Type, int (Type::* member)()>
std::cout << member << '\n';
template<int (* function)()>
std::cout << function << '\n';
The reason a pointer to a member function is different from a normal function pointer is because of the way the
this pointer is implemented.
When you write
object.function(), it’s actually implemented like
function(object) as if function were defined as
void function(Object * this).
C++'s is designed to keep the
this parameter implicit, and to make it difficult to provide an invalid object for
The kind of function call used on member functions is sometimes called a ‘thiscall’.