specialized template class question

2

2b|!2b==?

I have the ff template class:

template <class T1, class T2> class Periodic ;

I have two questions/problems

Q1).

I want to specialize it further into two template classes:

Periodic<Class1, T2> //specialized for class1
Periodic<Class2, T2> //specialized for class2


Q2)
Furthermore, I would like to typedef these new classes, e.g.

typedef Periodic<Class1, T2> MyTemplate1 ;

Is this possible ?
 
V

Victor Bazarov

2b|!2b==? said:
I have the ff template class:

template <class T1, class T2> class Periodic ;

I have two questions/problems

Q1).

I want to specialize it further into two template classes:

Periodic<Class1, T2> //specialized for class1
Periodic<Class2, T2> //specialized for class2


Yes.

tempalate<class T> class Periodic<Class1, T> { ...
templlate said:
Q2)
Furthermore, I would like to typedef these new classes, e.g.

typedef Periodic<Class1, T2> MyTemplate1 ;

Is this possible ?

Not yet. Search for "template typedefs" in the archives.

V
 
2

2b|!2b==?

Victor said:
Yes.

tempalate<class T> class Periodic<Class1, T> { ...
templlate<class T> class Periodic<Class2, T> { ...

Done. Thanks
Not yet. Search for "template typedefs" in the archives.

V

I looked this up, and came up with the ff struct declaration:

//The follwing is in myheader.h

template<class T>
struct MyStruct
{
typedef Periodic<Class1, T> Type1;
typedef Periodic<Class2, T> Type2;
};

MyStruct<Class1>::Type1 MyTypedef1 ;
MyStruct<Class2>::Type2 MyTypedef2 ;


//Now in my souce file, I have the following:

#include "myheader.h"

......

MyClass
{
public:
MyClass();
MyClass(const MyClass&);
.....
private:
MyTypedef1 m_type_def1 ; // <- compiler barfs here
};


What am I doing wrong ?
 
2

2b|!2b==?

Correction:

This :
template<class T>
struct MyStruct
{
typedef Periodic<Class1, T> Type1;
typedef Periodic<Class2, T> Type2;
};

MyStruct<Class1>::Type1 MyTypedef1 ;
MyStruct<Class2>::Type2 MyTypedef2 ;

Should have been posted as:

template<class T>
struct MyStruct
{
typedef Periodic<Class1, T> Type1 ;
typedef Periodic<Class2, T> Type2 ;
};

MyStruct<double> MyTypedef1 ;
MyStruct<double> MyTypedef2 ;
 
V

Victor Bazarov

2b|!2b==? said:
Correction:

This :


Should have been posted as:

template<class T>
struct MyStruct
{
typedef Periodic<Class1, T> Type1 ;
typedef Periodic<Class2, T> Type2 ;
};

MyStruct<double> MyTypedef1 ;
MyStruct<double> MyTypedef2 ;

You probalby meant

typedef MyStruct<double>::Type1 MyTypedef1;
typedef MyStruct<double>::Type2 MyTypedef2;

otherwise they are object declarations.

V
 
2

2b|!2b==?

Victor Bazarov wrote:

You probalby meant

typedef MyStruct<double>::Type1 MyTypedef1;
typedef MyStruct<double>::Type2 MyTypedef2;

otherwise they are object declarations.

V

Doh !
Of course, you're right - thanks for pointing out my glaring mistake -
time for a fresh cup of coffee maybe ? :p
 
V

Victor Bazarov

2b|!2b==? said:
Victor Bazarov wrote:



Doh !
Of course, you're right - thanks for pointing out my glaring mistake -
time for a fresh cup of coffee maybe ? :p

<shrug>

Either way, ou're not winning anything. You could do

template<class T> struct Periodic1 {
typedef Periodic<Class1,T> type;
};

and then use it to declare objects this way:

Periodic1<double>::type blah;

instead of remembering

Periodic<Class1,double> blah;

But I don't see much difference. You could of course use 'Periodic1'
(or 'MyStruct') inside another template... Again, not much better
than spelling it out.

I think template typedefs are coming in the next revision of the
language, though.

V
 
A

Amit Gupta

Without actually checking it ...

Wouldn't it be sufficient to say

template <typename T>
typedef typename Periodic<Class1, T> CP1;
 
R

red floyd

Amit said:
Without actually checking it ...

Wouldn't it be sufficient to say

template <typename T>
typedef typename Periodic<Class1, T> CP1;

No. Template typedefs are currently illegal. You need to put them
inside a class template or struct template.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top