473,241 Members | 1,678 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,241 software developers and data experts.

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 2506
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
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
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
4
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
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
by: edd | last post by:
Hello all, Please consider: template<typename Tclass my_class; template<class my_class<int> { //... };
1
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
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
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
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
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.