Connecting Tech Pros Worldwide Help | Site Map

Specialization of a method of a templated class

__PPS__
Guest
 
Posts: n/a
#1: Aug 5 '05
it's ok according to the standard to specialize member function like
this:

template<class T>struct xxx {
void func();
};
template<>bool xxx<int>::func(){
//int version
}
template<>bool xxx<double>::func(){
//double version
}

The problem is that I don't understand how to do same thing in this
case:

template<class C, class T>struct yyy {
C _c;
void func();
};

how is then I write implementation for yyy::func()??

I tried many ways but non of them worked for me


thanks

Victor Bazarov
Guest
 
Posts: n/a
#2: Aug 5 '05

re: Specialization of a method of a templated class


__PPS__ wrote:[color=blue]
> it's ok according to the standard to specialize member function like
> this:
>
> template<class T>struct xxx {
> void func();
> };
> template<>bool xxx<int>::func(){
> //int version
> }
> template<>bool xxx<double>::func(){
> //double version
> }[/color]

Yes, it's perfectly fine.
[color=blue]
>
> The problem is that I don't understand how to do same thing in this
> case:
>
> template<class C, class T>struct yyy {
> C _c;
> void func();
> };
>
> how is then I write implementation for yyy::func()??[/color]

template<> void yyy<int,double>::func() {
// int-double version
}

template<> void yyy<double,int>::func() {
// double-int version
}
[color=blue]
> I tried many ways but non of them worked for me[/color]

Hmm...

V
__PPS__
Guest
 
Posts: n/a
#3: Aug 5 '05

re: Specialization of a method of a templated class


I already found the answer to this question - it's not possible to do
what I wanted. (I wanted to do like template<class C>void
yyy<c,int>(){} - no partial specializations allowed for functions)
[color=blue]
>From here I have another question (which is a workaround to my original[/color]
problem)

suppose i have:
template<class X, class Y>class xxx{
template<bool> void yyy();
};

how is then I need to write implementations for the two versions of
yyy: yyy<true> and yyy<false>??

something like
template<class X,classY>
template<bool> xxx<X,Y>::yyy(){
/* false version */
}

template<class X,classY>
template<> xxx<X,Y>::yyy<true>(){
/* true version */
}
and it doesn't compile at yyy<true>

how do I do it correctly??

thanks

Victor Bazarov
Guest
 
Posts: n/a
#4: Aug 5 '05

re: Specialization of a method of a templated class


__PPS__ wrote:[color=blue]
> [...] I have another question (which is a workaround to my original
> problem)
>
> suppose i have:
> template<class X, class Y>class xxx{
> template<bool> void yyy();
> };
>
> how is then I need to write implementations for the two versions of
> yyy: yyy<true> and yyy<false>??
>
> something like
> template<class X,classY>
> template<bool> xxx<X,Y>::yyy(){
> /* false version */
> }
>
> template<class X,classY>
> template<> xxx<X,Y>::yyy<true>(){
> /* true version */
> }
> and it doesn't compile at yyy<true>
>
> how do I do it correctly??[/color]

Not possible.

To specialise a member you need to specialise the enclosing class _first_.

Here is a work-around:

template<class X, class Y> class xxx {
void yyy(bool b) { // not a template
if (b)
/* true version */
else
/* false version */
}

template<bool b> void yyy() {
yyy(b);
}
};

V
__PPS__
Guest
 
Posts: n/a
#5: Aug 5 '05

re: Specialization of a method of a templated class


my original template class is like this:
template<class,bool ZZZ> xxx{};
then there's templated yyy method which is used internally like this:
yyy<ZZZ>();
basicly if I wrote yyy(); and inside yyy I had if(ZZZ) {} else {} then
it would be compile time resolved if - else. But what I wanted to know
how can I do full specialization of templated method of a templated
class for the case

template<class X, class Y>class xxx{
template<bool> void yyy();
};

I only whant to know what's the right syntax etc...

Thanks

Victor Bazarov
Guest
 
Posts: n/a
#6: Aug 5 '05

re: Specialization of a method of a templated class


__PPS__ wrote:[color=blue]
> my original template class is like this:
> template<class,bool ZZZ> xxx{};
> then there's templated yyy method which is used internally like this:
> yyy<ZZZ>();
> basicly if I wrote yyy(); and inside yyy I had if(ZZZ) {} else {} then
> it would be compile time resolved if - else. But what I wanted to know
> how can I do full specialization of templated method of a templated
> class for the case
>
> template<class X, class Y>class xxx{
> template<bool> void yyy();
> };
>
> I only whant to know what's the right syntax etc...[/color]

Which word from "not possible" do you not understand?

To fully specialise a member template of a class template, you need to
specialise the class template first (14.7.3/18). Why do you insist on
my repeating what I already wrote?

V
__PPS__
Guest
 
Posts: n/a
#7: Aug 5 '05

re: Specialization of a method of a templated class


Thanks, everything is clear now

Closed Thread