Re: nested templates and partial specialization
On 11/12/2011 02:07 PM, ld wrote:
But it is not anymore a specialization for B<T> but just T, that is
why it works. Assume that another class C<T> wants to specialize it
too. How does it make the difference? Moreover, in this case, typename
and class have the same meaning (not always the case though).
Yes, right, sorry.
Actually I think you are trying a member specialisation by a weird
specialisation of the class (just the method, not whole class). It
works with sfinae, but not for virtual methods, and I am not sure
about partial class specialisations.
The below uses inheritance to get the example working - I don't know
if this fits your usage, but inheritance is one way of doing such
things.
Edek
// type declaration
template <typename T, typename I>
struct E; // line 3
// abstract class
struct D {
virtual void doit() const = 0;
};
template <class T, class TT> struct helper {
void doitInternal (T const&) const;
};
// partial specialization
template <class T>
struct E<T, D> : public helper<T,D> {
virtual void doit() const;
explicit E(T = T());
T& e;
};
// simple class
struct A {
void doit() const;
int a;
};
// template class
template <typename T>
struct B {
void doit() const;
T b;
};
// specialization with class, ok
template <>
void E<A, D>::doit() const
{
e.doit();
}
template <class T>
struct helper<B<T>, D> {
void doitInternal (B<T> const&) const;
};
template <class T>
void E<T, D>::doit() const
{
doitInternal(e);
}
template <class T, class TT>
void helper<T,TT>::doitInternal(T const&) const {}
template <class T>
void helper<B<T>, D>::doitInternal(B<T> const& e) const // line 43
{
// for B<T>
e.doit();
}
int main() {
E<A, D> a;
E<B<int>, D> b;
}