473,789 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1688
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_imp l() {} /* 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**********@r obotica.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
2535
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 of the currently matched element in the template, so that XPath expressions inside would be relative to that node? Specifically, I want to do this, in order to be able to issue xsl:call-template to apply some templates to some
0
934
by: Jim Douglas | last post by:
{System.Collections.Specialized.ListDictionary.NodeKeyValueCollection} : {System.Collections.Specialized.ListDictionary.NodeKeyValueCollection} Count: 25 ?entry.Contains("MachineName") true
4
7821
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. I have a vb.net windows forms project that is using a app.config file. the contents of the app.config is staggered. see a snippet below: <?xml version="1.0" encoding="utf-8" ?> <configuration>
28
2642
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 executable size. This should not cause any performance issue. So, is it safe to say that if I can offered to have bigger image size I can go ahead and use Templates with out worrying about any other issues?
5
1653
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
1820
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 boost::call_traits<T>::param_type param_type; typedef typename boost::call_traits<T>::reference reference;
2
1874
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 boost::call_traits<T>::param_type param_type; .... public:
0
2464
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 good search engines are there like Google, Yahoo and Live to name few of them. Though this search engines have extremely efficient, complex and beautiful algorithms designed by gems of the industry, but still they may not deliver best results for...
6
2945
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 there position in the array. This should be possible but I can't figure out exactly how to accomplish this. The below code should better illustrate what I'm trying to do: template <int I> class Item
0
9661
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10403
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10193
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9978
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9015
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5414
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2904
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.