This is legal in C++:
template <int N> class A {
void bar() {std::cout << N << '\n';}
};
template<>
void A<2>::bar() {std::cout << "Two\n";} // This is ok.
Now consider this 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 is the following illegal (placed after the above):
template <int... Rest>
void B<2, Rest...>::foo() { // Illegal.
std::cout << "Two ";
B<Rest...>::foo();
}
I don't see why B<2, Rest...>
is an incomplete type, as the error message states. So apparently, the only way to achieve what I want is 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 as above*/}
static void baz() {/*Same bunch of code as above*/}
};
Thus repeating all the code in bar() and baz()?