The problem is that you are trying to use the partial specialization of the entire class without specifying a partially specialized class.
If print
itself was a function template, the situation would be different, because you can really specialize function templates. However, your design only has the whole class as a template.
This means that template <typename T, int n> class Storage<T, n>
and template <int n> class Storage<int, n>
are completely different, unrelated classes. Thus, you must first define the last class:
template<int n> class Storage<int, n> {
Consider that the partial specialization Storage<int, n>
may be a completely different class from the main template and may have completely different member functions. The compiler does not know this until you define this class.
Following sbi comment, here is one idea:
//... in the class definition template<typename S, int m> friend void print_helper(const Storage<S, m> &); template<int m> friend void print_helper(const Storage<int, m> &); void print() { print_helper(*this); }
// outside: template <typename S, int size> void print_helper(const Storage<S, size> & s) { // ... } template <int size> void print_helper(const Storage<int, size> & s) { // ... }
Instead of friend
you can also create a static
helper function template, but this can add a lot of bloating code, since you will have two static function templates for the class type, and not just two globally.
Kerrek SB
source share