C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

specialization

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Amadeus W. M.
Guest





PostPosted: Fri Feb 24, 2006 5:06 pm    Post subject: specialization Reply with quote



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!
Back to top
Victor Bazarov
Guest





PostPosted: Fri Feb 24, 2006 6:06 pm    Post subject: Re: specialization Reply with quote



Amadeus W. M. wrote:
Quote:
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?

Quote:
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
Back to top
Amadeus W. M.
Guest





PostPosted: Fri Feb 24, 2006 9:06 pm    Post subject: Re: specialization Reply with quote



On Fri, 24 Feb 2006 12:31:02 -0500, Victor Bazarov wrote:

Quote:
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.
Back to top
Greg
Guest





PostPosted: Sat Feb 25, 2006 1:06 am    Post subject: Re: specialization Reply with quote

Amadeus W. M. wrote:
Quote:
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>
{

};

Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.