469,963 Members | 1,224 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

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 8988
__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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Paul MG | last post: by
8 posts views Thread by Agent Mulder | last post: by
4 posts views Thread by wakun | last post: by
6 posts views Thread by wkaras | last post: by
9 posts views Thread by Greg | last post: by
10 posts views Thread by jason.cipriani | last post: by
1 post views Thread by Ioannis Gyftos | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.