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

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>::operator 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 9151
__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::string>()const{ /* std::string case */ }
A a; B b;
public:
operator bool()const{ return op_bool<A>(); }
};

Jul 23 '05 #4
bl******@mail.ru 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::string>()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::string>()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<typename T, typename _unused>
struct _bool{ inline bool operator()(const T& v)const{ return
v!=T();} };
template<typename _unused>
struct _bool<std::string,_unused>{ inline bool operator()(const
std::string&v)const{ 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
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...
8
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...
5
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
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...
6
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...
9
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...
9
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... ...
10
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...
1
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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...
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.