c++ - Specializing a function of a template class -
this legal in c++:
template <int n> class { void bar() {std::cout << n << '\n';} }; template<> void a<2>::bar() {std::cout << "two\n";} // ok.
now consider class:
template <int...> struct b; template <int first, int... rest> struct b<first, rest...> : b<rest...> { static void foo() { std::cout << first << ' '; b<rest...>::foo(); } static void bar() {/*bunch of code*/} static void baz() {/*bunch of code*/} }; template <> struct b<> { static void foo() {} static void bar() {} static void baz() {} };
then why following illegal (placed after above):
template <int... rest> void b<2, rest...>::foo() { // illegal. std::cout << "two "; b<rest...>::foo(); }
i don't see why b<2, rest...>
incomplete type, error message states. apparently, way achieve want through this?
template <int... rest> struct b<2, rest...> : b<rest...> { static void foo() { std::cout << "two "; b<rest...>::foo(); } static void bar() {/*same bunch of code above*/} static void baz() {/*same bunch of code above*/} };
thus repeating code in bar() , baz()?
what trying achieve called partial template specialization, , allowed classes, not functions. see example why function template cannot partially specialized?
Comments
Post a Comment