I'm trying to store a pointer to an instance of the member function template Derived::initialize
as follows (see also rextester.com. For posterity, I've created a simpler version of the problem.):
class Base
{
public:
typedef void (Base::*setterFunction)( unsigned );
template<unsigned N>
struct SetterInterface
{
static Base::setterFunction Function;
};
protected:
template<unsigned N>
void setterImpl( unsigned )
{
}
};
template<unsigned N>
Base::setterFunction Base::SetterInterface<N>::Function = &Base::setterImpl<N>;
class Derived : public Base
{
public:
typedef void (Derived::*Initializer)();
template<typename T , void (T::*F)( unsigned ) >
void initialize()
{
}
template<typename C>
Derived( C* )
{
Initializer initializer = &Derived::initialize<C, C::template SetterInterface<0>::Function>; // NOT OK
//Initializer initializer = &Derived::initialize<C, C::template setterImpl<0> >; // OK
}
};
int main()
{
Derived derived( (Base*)0 );
}
But I'm getting the error message on GCC 5.4.0 (and 6.4.0)
Test.cpp: In instantiation of ‘Derived::Derived(C*) [with C = Base]’:
Test.cpp:45:28: required from here
Test.cpp:37:39: error: no matches converting function ‘initialize’ to type ‘Derived::Initializer {aka void (class Derived::*)()}’
Initializer initializer = &Derived::initialize<C, C::template SetterInterface<0>::Function>;
^
Test.cpp:30:7: note: candidate is: template<class T, void (T::* F1)(unsigned int)> void Derived::initialize()
void initialize()
The problem appears to lie with the member function template argument because C::template setterImpl<0>
works whereas C::template SetterInterface<0>::Function
(which I suppose to be an alias to the former) does not. For example:
Base::setterFunction f1 = &Base::setterImpl<0>;
Base::setterFunction f2 = Base::template SetterInterface<0>::Function;
The problem is that initialize()
non-type-template-parameter should be a constant expression; in your code it's a static member instead, so it cannot work; it could, if you declared it as static constexpr ( but you'd need at least a C++11 compiler then ):
class Base
{
protected:
template<unsigned N>
void setterImpl( unsigned );
public:
typedef void (Base::*setterFunction)( unsigned );
template<unsigned N>
struct SetterInterface
{
static constexpr Base::setterFunction Function = &Base::setterImpl<N>;
};
};