Specialized templates

R

Ruben Campos

I have a template class, and I want to create partially (totally)
specialized versions of it, by adding new methods to the original base
template specification. But it seems that partially (totally) specialized
templates are completely independent from their base templates, so in order
to do what I want, I have to redeclare and redefine (in both cases
duplicating code) all methods and attributes of the base template in each of
the partially (totally) specialized ones.

In the example below:

template <tyename T, int N>
class MyTemplate
{
public:
void f () { /* ... */ }
};

template <typename T>
class MyTemplate <T, 1>
{
public:
void g () { /* ... */ }
};

Is there any way to include f() in the interface of MyTemplate <T, 1>, other
than redeclaring and providing a copy of MyTemplate <T, N>::f()'s
implementation? I've tried to redeclare f() inside MyTemplate <T, 1>, and
provide only one implementation (the base template one's), but it doesn't
works. Thank you very much in advance.
 
R

Rob Williscroft

Ruben Campos wrote in in comp.lang.c++:
template <tyename T, int N>
class MyTemplate
{
public:
void f () { /* ... */ }
};

template <typename T>
class MyTemplate <T, 1>
{
public:
void g () { /* ... */ }
};

Is there any way to include f() in the interface of MyTemplate <T, 1>,
other than redeclaring and providing a copy of MyTemplate <T,
N>::f()'s implementation? I've tried to redeclare f() inside
MyTemplate <T, 1>, and provide only one implementation (the base
template one's), but it doesn't works. Thank you very much in advance.

template < typename T, int N >
struct MyTemplate_impl
{
void f() { }
protected:
~MyTemplate_impl() {} /* to keep the OOP's happy */
};

template < typename T, int N >
struct MyTemplate : MyTemplate_impl< T, N >
{
};

template < typename T >
struct MyTemplate< T, 1 > : MyTemplate_impl< T, 1 >
{
void g() { this->f(); /* for example */ }
};

Rob.
 
R

Ruben Campos

I've tried your answer, and it works well. Thank you very much, Rob.

But now, I have another question. Do you know if there is any problem with a
function declared as friend of a partially (totally) specialized template?
I've declared one, and while the other member functions are recognized
without problems, the friend one is not found by the linker, which returns a
external undefined symbol error.

For example, in the code below:

template <tyename T, int N>
class MyTemplate
{
/* ... */
};

template <typename T>
class MyTemplate <T,1>
{
public:
// Ok
MyTemplate <T,1> & operator+= (const MyTemplate <T,1> & value) { /* ...
*/ }

// Undefined external
friend MyTemplate <T,1> operator+ (const CMyTemplate <T,1> & value1,
const
CMyTemplate <T,1> & value2) { /* ... */ }
};
 
R

Ruben Campos

Sorry, I didn't realize that correct declaration for friend operator+ is
this:

friend MyTemplate <T,1> operator+ <TScalar> (const CMyTemplate <T,1> &
value1, const CMyTemplate <T,1> & value2) { /* ... */ }

So everything is fine. Thanks.
 

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,062
Latest member
OrderKetozenseACV

Latest Threads

Top