c++11 - Specialize static template function of base class -


i'm trying specialize static template function base class, , figured use case typedef/using statement. can't seem work, though. illegal, or syntax wrong?

#include <iostream>  class base { public:     template <typename t>     static t func () {         std::cout << (t)3.145 << std::endl;     } };  class derived : public base { public: //  using derivedfunc = base::func<int>; // doesn't work //  typedef base::func<int> derivedfunc; // nor     static constexpr auto derivedfunc = &base::func<int>; // seems work };  int main() {     base::func<double>(); // prints 3.145     derived::derivedfunc(); // prints 3     return 0; } 

the using or typedef expect type create type alias, passing value, namely pointer function. last line works that: auto deduced pointer assigned pointer function. becomes more explicit if write without auto:

static constexpr int(*derivedfunc)() = &base::func<int>; 

or if use this:

using derivedfunctype = decltype(&base::func<int>); static constexpr derivedfunctype derivedfunc = &base::func<int>; 

which in first line shows how required type function pointer , use type alias define actual member variable it.

live example

in cases, have function pointer. pointer static, hence pointer can accessed derived::derivedfunc , can called derived::derivedfunc().


Comments