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

Specialization of a method of a templated class

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
6 1949
__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
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
__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
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
__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
Thanks, everything is clear now

Aug 5 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
2
by: Nikolai Borissov | last post by:
Is it possible to specialize a templated class defined within another templeted class, like below: template<typename U> struct A { template<typename T> struct B; }; // Attempt to...
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
5
by: __PPS__ | last post by:
Hello, I want to write specialized method for a class: template<class A, class B> class xxx{ A a; B b; operator bool()const{ a==b; } };
3
by: Amadeus W. M. | last post by:
I have a class template <class Vector_t> class A; where Vector_t is some sort of vector. I want to provide different specializations for real and for complex vectors. I need the following: ...
3
by: Scott Frazer | last post by:
I'm trying to do some template specialization and can't get it to work quite right. I have a templated base class: template <typename T> class SignalBase { ... }; and a derived class: ...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
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:
1
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
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
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.