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

Specialized templates

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ruben Campos wrote in news:cn**********@peque.uv.es 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
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) { /* ... */ }
};
Jul 22 '05 #3

P: n/a
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.

"Ruben Campos" <Ru**********@robotica.uv.es> escribió en el mensaje
news:cn**********@peque.uv.es...
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) { /* ... */ }
};

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.