473,804 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

code duplication in template specialization

Hi,

I just need a specialization for only one member function of a template
class with _many_ members. Do I really have to duplicate the source code
for all the members, i.e. for those that do not need to be specialized?

E.g. in the example below, I'd like to avoid to redefine member B::g():

#include <stdio.h>
template<int x, typename T, short yclass B {
public:
void f(void) {printf("generi c f()\n");}
void g(void) {printf("generi c g()\n");}
};
// specialization for T == float
template<int x, short yclass B<x, float, y{
public:
void f(void) {printf("specia lized f()\n");}
void g(void) {printf("generi c g()\n");}
};
int main(void) {
B<1, int, 2b;
b.f();
b.g();
B<1, float, 2s;
s.f();
s.g();
}

Thanks for any suggestions,

Christof
Nov 27 '07 #1
3 2716
Christof Warlich wrote:
Hi,

I just need a specialization for only one member function of a
template class with _many_ members. Do I really have to duplicate the
source code for all the members, i.e. for those that do not need to
be specialized?
E.g. in the example below, I'd like to avoid to redefine member
B::g():
#include <stdio.h>
template<int x, typename T, short yclass B {
public:
void f(void) {printf("generi c f()\n");}
void g(void) {printf("generi c g()\n");}
};
// specialization for T == float
template<int x, short yclass B<x, float, y{
public:
void f(void) {printf("specia lized f()\n");}
void g(void) {printf("generi c g()\n");}
};
int main(void) {
B<1, int, 2b;
b.f();
b.g();
B<1, float, 2s;
s.f();
s.g();
}
Since you're defining a partial specialisation, you cannot avoid
completely redefining the contents of the class template. You can
split the original template in two and only redefine the class
that contains the member you need ot specialise:

#include <stdio.h>
template<typena me Tstruct Bf {
void f() {printf("generi c f()\n");}
};

template<int x, typename T, short yclass B : public Bf<T{
public:
void g() {printf("generi c g()\n");}
};

// specialization for T == float, but of 'Bf', not 'B'
template<struct Bf<float{
void f() {printf("specia lized f()\n");}
};

int main() {
B<1, int, 2b;
b.f();
b.g();
B<1, float, 2s;
s.f();
s.g();
}

Or you could simply define B<float>::f, and not specialise it:

// replace the Bf<floatspecial isation with
template<void Bf<float>::f() {printf("specia lized f()\n");}

since it's now a _full_ specialisation. ..

(and do drop the habit of putting 'void' inside parentheses, it's
a leftover from C, and isn't needed in C++).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 27 '07 #2
Christof Warlich wrote:
Hi,

I just need a specialization for only one member function of a template
class with _many_ members. Do I really have to duplicate the source code
for all the members, i.e. for those that do not need to be specialized?

E.g. in the example below, I'd like to avoid to redefine member B::g():

#include <stdio.h>
I'd use cstdio instead of stdio.h. Actually, I'd use iostreams instead
of printf, but...
template<int x, typename T, short yclass B {
public:
void f(void) {printf("generi c f()\n");}
void g(void) {printf("generi c g()\n");}
};
// specialization for T == float
template<int x, short yclass B<x, float, y{
public:
void f(void) {printf("specia lized f()\n");}
void g(void) {printf("generi c g()\n");}
};
I'd use inheritance to work around this I think:

//is saying short legal here? I'm not sure...
template <int x, typename T, short yclass Base {
public:
void g() { printf("generic g()\n"); }
};

template <int x, typename T, short yclass B : public Base<x,T,y{
public:
void f() {printf("generi c f()\n");}
};

template <int x, short yclass B<x, float, y: public Base<x, float, y{
public:
void f() {printf("specia lized f()\n");}
};

Quite where you choose to specialise, and where you chose to inherit
depends a bit on the actual problem in hand though obviously.
int main(void) {
B<1, int, 2b;
b.f();
b.g();
B<1, float, 2s;
s.f();
s.g();
}
Alan
Nov 27 '07 #3
thanks to all, yes, amazingly easy, inheritance does the trick ....
It's obviously been too late for me today already ;-)
Nov 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2931
by: Nikolai Borissov | last post by:
Is it possible to specialize a templated class defined within another templeted class, like below: template<typename U> struct A { template<typename T> struct B; }; // Attempt to specialize internal class B for type int
6
2020
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit instantiation of foo:
1
1995
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following: ****************************************************************** template<typename T> void from_string(const char Str, T &Obj); template<> void from_string(const char Str, long &); // template<> void from_string(const char Str, unsigned lon &); //
4
3641
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple inheritance (avoiding prosperation of virtual func tables). -- At the same time, a class taking N types shall contain a virtual member function that calls a function according to the number of arguments That means, something like:
9
2789
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload? Thank you. -- Marek
2
2498
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode: VertexValueTyp& vertex(int i); that dereferences vertices internally in case VertexTyp is a pointer
8
1950
by: mattias.nissler | last post by:
Hi! Here is a problem I ran into at work. The following example doesn't compile on gcc-4.1: struct cons_end {}; template<typename U,typename Vstruct cons { U elem; V tail;
9
3475
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. //------------------------------------------------------------------ // my regular glass class A { }; // my templated class
8
2971
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
0
9711
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
10343
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...
1
10335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
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
9169
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
7633
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
5529
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
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.