473,498 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class template specialization

I have a class template. Each of the instantiations implements a method
in the class template differently, so I (need?) to use template
speciaization.

My question is this, when writing the specialization, do I need to
implement only the method that is 'different', or do I need to implement
all the methods in the class template?
template <typename T1, typename T2>
class MyClass
{
void foo(const T1&, T2&) const;
...
//other methods follow below
int foobar(T1, T1, T2&);
// ...etc
};

template<>
class MyClass<int,double>
{
void foo(const int& i, double& d) const
{
//'specialized' logic here
}

/* Do I need all the other methods here ?
.... */
};
Nov 4 '08 #1
7 2530
On Nov 3, 7:22*pm, "(2b|!2b)==?" <void-s...@ursa-major.comwrote:
I have a class template. Each of the instantiations implements a method
in the class template differently, so I (need?) to use template
speciaization.

My question is this, when writing the specialization, do I need to
implement only the method that is 'different', or do I need to implement
all the methods in the class template?

template <typename T1, typename T2>
class MyClass
{
public:
* *void foo(const T1&, T2&) const;
* *...
* *//other methods follow below
* *int foobar(T1, T1, T2&);
* *// ...etc

};

template<>
class MyClass<int,double>
{
public:
* * *void foo(const int& i, double& d) const
* * *{
* * * * //'specialized' logic here
* * *}

* * /* Do I need all the other methods here ?
* * *.... */

};
Yes, You'll need to provide specialization for the entire type.
To partially specialize a template, derive from the generic one:

class DerivedClass : public MyClass< int, double >
{
public:
void foo(const int& i, double& d) const
{
// do specialized stuff here
}
};

foo(...) now overides any foo(...) in the base class and foobar() is
available.
This works too:

template< typename N = int, typename D = double >
class DerivedClass : public MyClass< N, D >
{
public:
void foo(const N& i, D& d) const
{
std::cout << "DerivedClass::foo(const int&, double&) const\n";
}
};

DerivedClass< instance;
instance.foobar(...);

Nov 4 '08 #2
On 4 Nov, 01:22, "(2b|!2b)==?" <void-s...@ursa-major.comwrote:
I have a class template. Each of the instantiations implements a method
in the class template differently, so I (need?) to use template
speciaization.

My question is this, when writing the specialization, do I need to
implement only the method that is 'different', or do I need to implement
all the methods in the class template?

template <typename T1, typename T2>
class MyClass
{
* *void foo(const T1&, T2&) const;
* *...
* *//other methods follow below
* *int foobar(T1, T1, T2&);
* *// ...etc

};
If only the function body is different, you could simply do this:

template<>
void MyClass<int, double>::foo(const int&, double&) const
{
// ...
}
template<>
void MyClass<double, double>::foo(const double&, double&) const
{
// ...
}
Nov 4 '08 #3
Triple-DES wrote:
On 4 Nov, 01:22, "(2b|!2b)==?" <void-s...@ursa-major.comwrote:
>I have a class template. Each of the instantiations implements a method
in the class template differently, so I (need?) to use template
speciaization.

My question is this, when writing the specialization, do I need to
implement only the method that is 'different', or do I need to implement
all the methods in the class template?

template <typename T1, typename T2>
class MyClass
{
void foo(const T1&, T2&) const;
...
//other methods follow below
int foobar(T1, T1, T2&);
// ...etc

};

If only the function body is different, you could simply do this:

template<>
void MyClass<int, double>::foo(const int&, double&) const
{
// ...
}
template<>
void MyClass<double, double>::foo(const double&, double&) const
{
// ...
}
That would be explicit instantiation, or what's that called
officially?

Schobi
Nov 7 '08 #4
Hendrik Schober wrote:
>>template <typename T1, typename T2>
class MyClass
{
void foo(const T1&, T2&) const;
...
//other methods follow below
int foobar(T1, T1, T2&);
// ...etc

};

If only the function body is different, you could simply do this:

template<>
void MyClass<int, double>::foo(const int&, double&) const
{
// ...
}
template<>
void MyClass<double, double>::foo(const double&, double&) const
{
// ...
}

That would be explicit instantiation, or what's that called
officially?
That's called "explicit specialization" (the 'template<>' bit is usually
a dead giveaway). When it comes to explicit specialization, the class
itself and the members of the class are pretty much independent
templates. You can perform explicit specialization on them
independently. You can explicitly specialize the entire class template
(meaning that you'll have to redefine the whole thing from scratch), or
you can explicitly specialize the members (without specializing the
entire class). The latter is what's done in the above code.

Of course, once you decided to explicitly specialize the entire class
template for type 'T', you can no longer explicitly specialize just the
members for the same type 'T'.

--
Best regards,
Andrey Tarasevich
Nov 8 '08 #5
Andrey Tarasevich wrote:
Hendrik Schober wrote:
>>>template <typename T1, typename T2>
class MyClass
{
void foo(const T1&, T2&) const;
...
//other methods follow below
int foobar(T1, T1, T2&);
// ...etc

};
If only the function body is different, you could simply do this:

template<>
void MyClass<int, double>::foo(const int&, double&) const
{
// ...
}
template<>
void MyClass<double, double>::foo(const double&, double&) const
{
// ...
}
That would be explicit instantiation, or what's that called
officially?

That's called "explicit specialization" (the 'template<>' bit is usually
a dead giveaway). When it comes to explicit specialization, the class
itself and the members of the class are pretty much independent
templates. You can perform explicit specialization on them
independently. You can explicitly specialize the entire class template
(meaning that you'll have to redefine the whole thing from scratch), or
you can explicitly specialize the members (without specializing the
entire class). The latter is what's done in the above code.

Of course, once you decided to explicitly specialize the entire class
template for type 'T', you can no longer explicitly specialize just the
members for the same type 'T'.
Oh, I thought explicit specialization of class template members
wasn't allowed? Oh wait, that was explicit specialization of
member templates, right? <sigh>

Schobi
Nov 9 '08 #6
Hendrik Schober wrote:
>
Oh, I thought explicit specialization of class template members
wasn't allowed? Oh wait, that was explicit specialization of
member templates, right? <sigh>
Right. It is a completely different issue.

BTW, even that is allowed, as long as you explicitly specialize the
enclosing template as well

template <class Tstruct C {
template <class Ustruct D {};
};

template<template<struct C<int>::D<double{}; // OK

--
Best regards,
Andrey Tarasevich

Nov 9 '08 #7
Andrey Tarasevich wrote:
Hendrik Schober wrote:
> Oh, I thought explicit specialization of class template members
wasn't allowed? Oh wait, that was explicit specialization of
member templates, right? <sigh>

Right. It is a completely different issue.

BTW, even that is allowed, as long as you explicitly specialize the
enclosing template as well

template <class Tstruct C {
template <class Ustruct D {};
};

template<template<struct C<int>::D<double{}; // OK
Thanks.
So the only thing disallowed would be explicit specialization
of member templates of unspecialized class templates?

Schobi
Nov 9 '08 #8

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

Similar topics

6
2624
by: jesse | last post by:
I am frustrated by class specialization. i don't think it helps me a lot. suppose we have template <class T> class Talkative { T& t; public:
2
5756
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
4
2565
by: SainTiss | last post by:
Hi, From what I've read in several places, it seems that explicit specialization of member functions of class templates is allowed, but partial specialization isn't: template<class T, class...
1
2253
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
5
1848
by: edd | last post by:
Hello all, Please consider: template<typename Tclass my_class; template<class my_class<int> { //... };
1
1600
by: toton | last post by:
Hi, I am doing some template specialization for a template class, where specialization is done on a member function. I am not able to get exact syntax for it. The code is give as below, enum...
2
7672
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
8
2929
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 { // ... };
5
3717
by: huili80 | last post by:
For example, like in the following, the part commented out was intended as partial spectialzation, but it would even compile. Is it even legal to partially specialize a nested template class...
0
7162
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,...
0
7197
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...
1
6881
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...
1
4899
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...
0
4584
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...
0
3088
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
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...

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.