Topic: Specializing templates with complex<T> or other templates
Author: "Martijn Lievaart" <nospam@somewhere.org>
Date: 1998/03/16 Raw View
(snipped original, to long to repeat)
I don't know about the standard, but OTOH, I would suggest trying
class Base
{
// common stuff here
};
template <class T>
class foo : public Base
{
public:
void doSomething();
};
template <class T> class foo< complex<T> >
class foo : public Base
{
public:
void doSomething();
};
Hope this works and helps,
Martijn
(mlievaar(at)orion(dot)nl)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: John Aldridge <jpsa@jjdash.demon.co.uk>
Date: 1998/03/17 Raw View
In article <6eivbp$cn5$1@news2.xs4all.nl>, Martijn Lievaart
<nospam@somewhere.org> writes
>(snipped original, to long to repeat)
>
>I don't know about the standard, but OTOH, I would suggest trying
>class Base
>{
> // common stuff here
>};
>
>template <class T>
>class foo : public Base
>{
> public:
> void doSomething();
>};
>
>template <class T> class foo< complex<T> >
>class foo : public Base
>{
> public:
> void doSomething();
>};
It doesn't help much, unfortunately, because the common methods often
need to return a result of the template type. Consider a class
representing D dimensioned mathematical vectors.
template<int D> class Vec {
public:
Vec &operator += (Vec& r);
Vec &operator -= (Vec& r);
Vec &operator *= (double r);
Vec &operator /= (double r);
// and about a million other methods common to Vecs of all dimensions
};
Now, how do I add the following:
friend double Vec<2>::crossProduct (const Vec<2> &l, const Vec<2> &r);
friend Vec<3> Vec<3>::crossProduct (const Vec<3> &l, const Vec<3> &r);
(note the different return types). Hmm, maybe I can hack this with a
traits class:
template<int D> class VecTraits {
};
template<> class VecTraits<2> { // Is that the new syntax?
public:
typedef double CrossProductResultType;
};
template<> class VecTraits<3> {
public:
typedef Vec<3> CrossProductResultType;
};
and then write:
:
friend VecTraits<D>::CrossProductResultType
crossProduct (const Vec<D> &l, const Vec<D> &r);
:
in the (unspecialised) template class. Yuck.
Even this doesn't help for methods which are only present in one
specialisation:
int Vec<2>::quadrant () const; // 1->+X/+Y, 2->+X/-Y ...
or for methods which take different numbers of arguments such as the
specialised ctors:
Vec<2>::Vec (double x, double y);
Vec<3>::Vec (double x, double y, double z);
I think you're stuck with repeating all the common stuff in each
specialisation. I hope I'm wrong...
--
Cheers,
John
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Martijn Lievaart" <nospam@somewhere.org>
Date: 1998/03/17 Raw View
I bow my head. I tried lots of things, but it just seems impossible. Too
bad. (but I'll keep trying ;^) )
I remember that there appeared a package in Dr.Dobbs some time ago that does
just what you want. Anyone remember this?
Martijn
(mlievaar(at)orion(dot)nl)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]