[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Class template specialization

(2b|!2b)==?

11/4/2008 12:23:00 AM

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 ?
.... */
};
8 Answers

Salt_Peter

11/4/2008 4:21:00 AM

0

On Nov 3, 7:22 pm, "(2b|!2b)==?" <void-s...@ursa-major.com> wrote:
> 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(...);

Triple-DES

11/4/2008 5:48:00 AM

0

On 4 Nov, 01:22, "(2b|!2b)==?" <void-s...@ursa-major.com> wrote:
> 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
{
// ...
}

Hendrik Schober

11/7/2008 9:47:00 AM

0

Triple-DES wrote:
> On 4 Nov, 01:22, "(2b|!2b)==?" <void-s...@ursa-major.com> wrote:
>> 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

Andrey Tarasevich

11/8/2008 9:36:00 PM

0

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

Hendrik Schober

11/9/2008 12:24:00 AM

0

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

Andrey Tarasevich

11/9/2008 5:10:00 AM

0

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 T> struct C {
template <class U> struct D {};
};

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

--
Best regards,
Andrey Tarasevich

Hendrik Schober

11/9/2008 12:14:00 PM

0

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 T> struct C {
> template <class U> struct 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

Evertjan.

6/9/2014 10:37:00 PM

0

Shelly <sheldonlg@thevillages.net> wrote on 09 jun 2014 in
soc.culture.jewish.moderated:

> On 6/9/2014 3:03 PM, Evertjan. wrote:
>> mm <mm2005@bigfoot.com> wrote on 09 jun 2014 in
>> soc.culture.jewish.moderated:
>>
>>>> When saying "our" and not meaning the NG,
>>>> one should specify the context.
>>>
>>> Others can always ask. As you did.
>>
>> True.
>>
>> However, you only can when the posting is recent.
>
> Why? So long as the poster to whom you are responding still posts here
> (so that you can get an answer), what is stopping you from asking about
> an old post? If that poster is not here any longer, what is preventing
> you from _asking_?

True, but very very ineffective.

I maintain that, if a poster has any other meaning for "we" than the
collective membership of the newsgroup, he/she should specify.

Since this collectivity does not own soldiers, well ...

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)