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.
There are no partial specialisations of function templates.
You may be able to work around that limitation by using overloading.
V
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.
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