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 ?
.... */
}; 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(...);
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
{
// ...
}
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
|
by: Jeff |
last post by:
/* --------------------------------------------------------------------------
Hello,
I was experimenting with class templates and specializing member
functions and came across a simple problem...
|
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...
|
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...
|
by: edd |
last post by:
Hello all,
Please consider:
template<typename Tclass my_class;
template<class my_class<int>
{
//...
};
| |
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...
|
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:
|
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
{
// ...
};
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |