"Inheriting" during template specialisation

A

Adam Nielsen

Hi again,

I've got another question about template specialisation. I would like
to declare some data types in the main template (the "base class") but I
would then like to extend the behaviour with template specialisations.
Unfortunately it seems that when I try this, the specialisation doesn't
get appended to the base template (as happens when you inherit from a
base class), but rather it replaces it completely, for example this code
won't compile:

template <typename T>
class A
{
public:
typedef std::vector<T> V;
//V myVector; // works
};

class A<int>
{
private:
V intVector; // doesn't work
};


I get this error:

'V' is used as a type, but is not defined as a type.

Which implies that the typedef is getting lost. Is there a way around
this? Some sort of class inheritance would probably work, but I'd like
to try avoiding this as it's more for code clarity than anything else
('int' in my example above is actually a complex template instantiation,
so the code can become difficult to read if it's written out in full all
over the place.)

Thanks,
Adam.
 
X

xtrigger303

Hi again,

I've got another question about template specialisation. I would like
to declare some data types in the main template (the "base class") but I
would then like to extend the behaviour with template specialisations.
Unfortunately it seems that when I try this, the specialisation doesn't
get appended to the base template (as happens when you inherit from a
base class), but rather it replaces it completely, for example this code
won't compile:

template <typename T>
class A
{
public:
typedef std::vector<T> V;
//V myVector; // works
};

class A<int>
{
private:
V intVector; // doesn't work
};

I get this error:

'V' is used as a type, but is not defined as a type.

Which implies that the typedef is getting lost. Is there a way around
this? Some sort of class inheritance would probably work, but I'd like
to try avoiding this as it's more for code clarity than anything else
('int' in my example above is actually a complex template instantiation,
so the code can become difficult to read if it's written out in full all
over the place.)

Thanks,
Adam.

Hi,
IMHO you have to inherit to maximize code reuse. Cannot think of any
other way...
I usually do something like

#include <iostream>
#include <vector>

class CGenericUntyped
{
public:
// very generic stuff
typedef int tWhatever;
static int const kSomeConstant = 10;
};

//

template< typename T >
class CGenericTyped : public CGenericUntyped
{
public:
// stuff dependent on a templated type
typedef std::vector< T > tVector;

private:

tVector mVec;
};

//

template< typename T >
class CGeneric : public CGenericTyped< T >
{
public:
// unspecialized version
void Do( void ) { std::cout << "DO SOMETHING\n"; }
};

//

template< >
class CGeneric< int > : public CGenericTyped< int >
{
public:
// specialized version
void Do( void ) { std::cout << "DO SOMETHING INT\n"; }
};

int main( void )
{
CGeneric< double > obj1;
obj1.Do();
CGeneric< int > obj2;
obj2.Do();
}
 
A

Adam Nielsen

IMHO you have to inherit to maximize code reuse. Cannot think of any
other way...

Fair enough, that makes sense - thanks for your reply!

Cheers,
Adam.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,068
Latest member
MakersCBDIngredients

Latest Threads

Top