Re: nested templates and partial specialization
On 11/12/2011 12:33 PM, ld wrote:
On Nov 12, 10:16 am, Edek<edek.pienkow...@gmail.com> wrote:
On 11/12/2011 08:41 AM, ld wrote:
template<> template<typename T>
void
E<B<T>, D>::doit() const // faulty line
Your notation is for
template<>
template<class T>
E<int, float>::doit<T>()
(or the other way round, I always forget if the outer
template is for the class or member).
The outer is for the class and the inner is for the class member but
in my case the two are for the class. You can remove safely the outer
template<>, it changes nothing to the problem. I remember that few
months ago somebody showed a similar problem and got an answer but I
cannot find it again. The answer was containing a use of template in
an uncommon place.
The code posted below works, but you need a second E partial
specialisation explicitly. You would need the original
method specialised for <T,D> if you need direct T too.
I can't really understand why E<Something<some_T>, D> does not
match struct E<T,D> - which would be nice, and I assume which
is what you want.
Edek
// type declaration
template <typename T, typename I>
struct E; // line 3
// abstract class
struct D {
virtual void doit() const = 0;
};
// partial specialization
template <typename T>
struct E<T, D> {
virtual void doit() const;
explicit E(T = T());
T& e;
};
// 2nd partial specialisation
template <template<class> class Obj, class T>
struct E<Obj<T>, D> {
typedef Obj<T> TT;
virtual void doit () const;
explicit E(TT = TT());
TT&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();
}
// specialization with template class, error
template <template<class> class Wrap, class T>
void E<Wrap<T>, D>::doit() const // line 43
{
e.doit();
}
int main() {
E<A, D> a;
E<B<int>, D> b;
}