473,841 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Partial template specialization for a method

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;
}
};

What I want to do is to be able to specialize operator bool for the
case when class A=std::string, so that in that case it returned
a.length()>0;

For this to work I have a choice to copy my class xxx definition like
this:

template<class B>
class xxx<std::string , B>{
std::string a; B b;
operator bool()const{
a.length()>0;
}
};

But I have a large class where I need to specialize several methods.
What I want to do is to specialize only the method operator bool:

template<class A, class B>
class xxx{
A a; B b;
operator bool()const;
};

//default implementation
template<class A, class B>
xxx<A,B>::opera tor bool()const{
return a==b;
}

//specialized method, doesn't compile
template<class B>
xxx<std::string , B>::operator bool()const{
return a.length()>0;
}

but it doesn't work like this. What's the mistake, how to do it the
right way?
I use vs2003

Thank you.
Jul 23 '05 #1
5 9187
__PPS__ wrote:
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;
}
};
[...]


There are no partial specialisations of function templates.

You may be able to work around that limitation by using overloading.

V
Jul 23 '05 #2
I meant explicit specialization of a method in case if the first
template argument is an std::string
Have no idea how overloading can help in this case (I think it would be
useless overhead in terms of lines of code and it wouldn't prevent me
from using default operator bool if I one day forgot about overloaded
class for std::string) but if ther's no way to do that, then the best
way is do it the way I described in my post (to copy class
declaration...) , but whenever I want to modify class I also need to
update all copies of it... seems like there must be a better way to do
what I want...

Jul 23 '05 #3
I fond a workaround:

template<class A, class B>
class xxx{
template<class C>bool op_bool()const{ /* default implementation */ }
template<>bool op_bool<std::st ring>()const{ /* std::string case */ }
A a; B b;
public:
operator bool()const{ return op_bool<A>(); }
};

Jul 23 '05 #4
bl******@mail.r u wrote:
I fond a workaround:

template<class A, class B>
class xxx{
template<class C>bool op_bool()const{ /* default implementation */ }
template<>bool op_bool<std::st ring>()const{ /* std::string case */ }
A a; B b;
public:
operator bool()const{ return op_bool<A>(); }
};


Not legal IIRC. Specialisations shall not be defined inside the class.
Jul 23 '05 #5
I found the same argument why it was disallowed in g++ - using g++33
and g++34 I cannot compile this code, however it works with vs2003 and
vc++ 8. As far as I'm concerned it also works with intel compiler.
(http://lists.debian.org/debian-gcc/2.../msg00015.html)

trying to move the line
template<>bool op_bool<std::st ring>()const{ /* std::string case */ }
outside class definition brought even worse problem than what I
originally started this thread with.

If suppose you are correct on that case, then what sort of cryptic code
I need to write to put this specialization outside the class body? At
least I don't see any reason for the standard to force to put this sort
of specialization outside the class body...

ms compiler help: "C++ member templates are supported as long as they
are fully defined within the enclosing class...."
meaning that I cannot even put implementation of a templated method of
a templated class outside of the body of the class with ms compier.
According to the link about g++, there's a way to make it work - define
inner struct with unused template parameter and do a partial
specialization instead of full specialization. .. seems to be a really
ugly solution for my original problem...
template<typena me T, typename _unused>
struct _bool{ inline bool operator()(cons t T& v)const{ return
v!=T();} };
template<typena me _unused>
struct _bool<std::stri ng,_unused>{ inline bool operator()(cons t
std::string&v)c onst{ return v.length()>0;}} ;

operator bool(){ return _bool<A,void>(a ); }

works with g++ also, but is really ugly IMO

Jul 23 '05 #6

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

Similar topics

17
6871
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()).
8
7693
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I specialize Music<Bach>, I expect that the original play() method is available in the specialization, but it is not. How can I fix this? -X
5
6594
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
4
1504
by: wakun | last post by:
Hi there, I am learning template programming. When testing the partial specialization, I have some probelms Here is a full templated class template <typename T, int n> class CT { public: T data;
6
2769
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave no errors compiling this:
9
2791
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload? Thank you. -- Marek
9
1965
by: Greg | last post by:
Hi, I would like to specify behavior of a class member relatively to template implemetation. It works in usual cases but it seems to fail with to templates when one of the two is specified... Exemple: template <class T, int Num> class A
10
2799
by: jason.cipriani | last post by:
I never seem to be able to get this right. Here I have some code: template <typename T, int Nclass A { void f (T); }; template <typename Tvoid A<T,1>::f (T) { } template <typename Tvoid A<T,3>::f (T) {
1
2231
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
9706
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
10927
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
10609
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
10667
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
10303
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7848
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7025
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5691
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
4498
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

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.