|
Boost : |
From: Sam Partington (Sam.Partington_at_[hidden])
Date: 2003-02-25 09:19:05
Daniel Frey wrote:
> Nice idea! I already had something similar in mind, but you managed to
> make it really generic.
Not really, all I've done is to borrow the code from shared_ptr, and to put
into a shape like one of the existing unary operator helpers in
operator.hpp. Thanks anyway though :-)
> The only problem I see is that an instance of safe_bool_conversion is
> created which is not really needed. I suggest to rely on the operator!
> provided by T:
>
> template< class T, class B = ::boost::detail::empty_base >
> struct bool_testable : B
> {
> private:
> typedef bool (T::*unspecified_bool_type)() const;
>
> public:
> operator unspecified_bool_type() const
> {
> return !static_cast< const T& >( *this ) ? 0 : &T::operator!();
> }
> };
I thought of this too, but this limits the user to using a member based
operator!. So I couldn't do this :
class A : public boost::bool_testable<A>
{
public:
int get();
};
bool operator!(const A& a)
{
return a.get() == 0;
}
Of course I've never actually wanted to do that, so its maybe not a problem.
After all the conversion operator itself has to be a member, so it probably
isn't much of a restriction at all.
Then again, how much does the safe_bool_conversion function cost?
Sam
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk