By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,915 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,915 IT Pros & Developers. It's quick & easy.

Specialization of a method of a templated class

P: n/a
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

Aug 5 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
__PPS__ wrote:
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
}
Yes, it's perfectly fine.

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()??
template<> void yyy<int,double>::func() {
// int-double version
}

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


Hmm...

V
Aug 5 '05 #2

P: n/a
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)
From here 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??

thanks

Aug 5 '05 #3

P: n/a
__PPS__ wrote:
[...] 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??


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
Aug 5 '05 #4

P: n/a
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

Aug 5 '05 #5

P: n/a
__PPS__ wrote:
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...


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
Aug 5 '05 #6

P: n/a
Thanks, everything is clear now

Aug 5 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.