473,408 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Specialized templates

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
3 1665
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
0
by: Jim Douglas | last post by:
{System.Collections.Specialized.ListDictionary.NodeKeyValueCollection} : {System.Collections.Specialized.ListDictionary.NodeKeyValueCollection} Count: 25 ?entry.Contains("MachineName") ...
4
by: Kerr | last post by:
Hi all, I've been scouring the internet for help with this problem and every occurance i've seen reconstructs the problem but no one seems to have a solution. Hoping that you guys can help me. ...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
5
by: desktop | last post by:
I have this example: template<class T(1) void f( T ); template<class T(2) void f( T* ); template< (3)
4
by: Olaf | last post by:
Hi, I'm writing a small wrapper for libcurl. For Options I use: template<typename T, long CURLOPT_ID> class CurlOption: boost::noncopyable { public: typedef typename...
2
by: Olaf | last post by:
Hi, I'm working on a thin libcurl layer. Therefore I have the following classes: template<typename T, long CURLOPT_ID> class CurlOption: boost::noncopyable { public: typedef typename...
0
by: passion | last post by:
"Specialized Search Engines" along with Google Search Capability (2 in 1): http://specialized-search-engines.blogspot.com/ Billions of websites are available on the web and plenty of extremely...
6
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.