"Kalle Rutanen" <no**@here.com> wrote in message
news:MP************************@news.cc.tut.fi
Here is a short code. I can't see why the template specialization does
not work (vsnet2003)? Does the standard allow these kind of
specializations?
No, it doesn't.
template <int i>
class B {};
template <typename T>
class A
{
public:
void f();
};
So A has a type parameter.
template <typename T>
void A<T>::f()
{
}
template <int i>
void A<B<i> >::f()
{
}
Now, you are trying to give it a non-type parameter. This isn't really
specialization at all. It is a change in the nature of the template from one
having a type parameter to one having a non-type parameter.
int main()
{
A<B<2> > a;
a.f();
return 0;
}
Your example doesn't give much indication what you are trying to achieve.
Making a guess, something resembling what you want might be achieved
with partial specialisation, as follows:
#include <iostream>
using namespace std;
template <int i>
class B {};
template <typename T, int i=0>
class A
{
public:
void f();
};
template <typename T, int i>
void A<T,i>::f()
{
cout << "Member function of general class\n";
}
// We can't partially specialize a function,
// only a class, so we partially specialize the class
// to depend on int only
template <int i>
class A<B<i> >
{
public:
void f();
};
// Now we can define the member function of the partially
// specialized class.
template <int i>
void A<B<i> >::f()
{
cout << "Member function of partially specialized class\n";
}
int main()
{
A<char *> a1;
A<B<2> > a2;
a1.f();
a2.f();
return 0;
}
--
John Carson