By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,900 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,900 IT Pros & Developers. It's quick & easy.

specialization

P: n/a
I have a class

template <class Vector_t>
class A;

where Vector_t is some sort of vector. I want to provide different
specializations for real and for complex vectors. I need the following:

// Real vectors:
A< valarray<double> >; // templated
A< MyRealVector >; // non-tempalted

// Complex vectors:
A< vector< complex<double> >; // templated
A< MyComplexVector >; // non-templated.

Is this possible? How would I do that? Bad design? What's the right way?
Thanks!

Feb 24 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Amadeus W. M. wrote:
I have a class

template <class Vector_t>
class A;

where Vector_t is some sort of vector. I want to provide different
specializations for real and for complex vectors. I need the following:

// Real vectors:
A< valarray<double> >; // templated
What do you mean by the word 'templated' in the comment?
A< MyRealVector >; // non-tempalted

// Complex vectors:
A< vector< complex<double> >; // templated
A< MyComplexVector >; // non-templated.

Is this possible? How would I do that? Bad design? What's the right way?


Have you tried? What's the result? If you haven't, what's stopping you?
What book are you reading that doesn't tell how to specialise a template?

template<> class A<MyRealVector> { /* specialisation */ };

or

typedef A<MyRealVector> MyRealA;

They give you different results, of course. The former would require you
to actually specify the inner workings (which are supposedly different
from the original template), the latter _uses_ the inner workings of the
original template.

V
--
Please remove capital As from my address when replying by mail
Feb 24 '06 #2

P: n/a
On Fri, 24 Feb 2006 12:31:02 -0500, Victor Bazarov wrote:
Amadeus W. M. wrote:
I have a class

template <class Vector_t>
class A;

where Vector_t is some sort of vector. I want to provide different
specializations for real and for complex vectors. I need the following:

// Real vectors:
A< valarray<double> >; // templated


What do you mean by the word 'templated' in the comment?
A< MyRealVector >; // non-tempalted

// Complex vectors:
A< vector< complex<double> >; // templated
A< MyComplexVector >; // non-templated.


Sorry, I didn't explain very well.
I mean that the template argument of A is itself a template.

So I have the most general

template <class Vector_t>
class A
{
};

I don't want to specialize it to a particular vector type, such as
valarray<double> or MyNonTemplatedVector. The specializations differ in
the SCALAR type of the vector, but should be irrespective of the actual
container. So what I'd like would be rather

template <class Scalar_t, template<class> class Vector_t>
class A
{
// work on Vector_t<Scalar_t>;
};

Then this should somehow be specialized (1) to Scalar_t = float/double and
(2) to Scalar_t = complex<double>, for instance, but still have the
container Vector_t as a parameter.

I'm reading Stroustrup's book. Can this be done?

Even if it CAN be done, with a template Vector_t I won't be able to have a

A<MyNonTemplatedVector>;
Maybe I should just do

template <class Vector_t>
class RealA
{

};

template <class Vector_t>
class ComplexA
{

};

Then Vector_t is the most general, and I have two implementations - one
for real, one for complex. It won't be transparent to the user though.
Feb 24 '06 #3

P: n/a

Amadeus W. M. wrote:
On Fri, 24 Feb 2006 12:31:02 -0500, Victor Bazarov wrote:
Amadeus W. M. wrote:
I have a class

template <class Vector_t>
class A;

where Vector_t is some sort of vector. I want to provide different
specializations for real and for complex vectors. I need the following:

// Real vectors:
A< valarray<double> >; // templated
What do you mean by the word 'templated' in the comment?
A< MyRealVector >; // non-tempalted

// Complex vectors:
A< vector< complex<double> >; // templated
A< MyComplexVector >; // non-templated.


Sorry, I didn't explain very well.
I mean that the template argument of A is itself a template.

So I have the most general

template <class Vector_t>
class A
{
};

I don't want to specialize it to a particular vector type, such as
valarray<double> or MyNonTemplatedVector. The specializations differ in
the SCALAR type of the vector, but should be irrespective of the actual
container. So what I'd like would be rather

template <class Scalar_t, template<class> class Vector_t>
class A
{
// work on Vector_t<Scalar_t>;
};

Then this should somehow be specialized (1) to Scalar_t = float/double and
(2) to Scalar_t = complex<double>, for instance, but still have the
container Vector_t as a parameter.

I'm reading Stroustrup's book. Can this be done?


Yes:

template <class Scalar_t, template<class> class Vector_t>
class A
{
// work on Vector_t<Scalar_t>;

};

template < template<class> class Vector_t>
class A<double, Vector_t>
{

};
Even if it CAN be done, with a template Vector_t I won't be able to have a

A<MyNonTemplatedVector>;


You can if you declare another "A" class template, in addition to the
first.

Greg

Feb 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.