472,358 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 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 9085
__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{
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.