473,804 Members | 2,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::fu nc(){
//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 1974
__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>::fu nc(){
//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<t rue>(){
/* 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<t rue>(){
/* 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
6869
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 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
2
2931
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 specialize internal class B for type int
3
2733
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 Foo> class Bar { private: Foo foo;
5
9186
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
1315
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: // Real vectors: A< valarray<double> >; // templated
3
362
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: template <typename T> class Signal : public SignalBase<T> {
9
3474
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. //------------------------------------------------------------------ // my regular glass class A { }; // my templated class
2
7703
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
2229
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9571
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10561
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5505
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.