Boost logo

Boost :

Subject: Re: [boost] safe bool not safe enough ?
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2013-03-14 09:13:41


2013/3/14 John Maddock <boost.regex_at_[hidden]>

> Just discovered (oops!) that the so called safe bool ideom is not as safe
> as it appears, for example:
>
> class Testable
> {
> bool ok_;
> typedef void (Testable::*bool_type)() const;
> void this_type_does_not_support_**comparisons() const {}
> public:
> explicit Testable(bool b=true):ok_(b) {}
>
> operator bool_type() const
> {
> return ok_==true ?
> &Testable::this_type_does_not_**support_comparisons : 0;
> }
> };
>
> Testable t(true);
>
> bool b = t; // Compiles OK - Ooops!
>

I'm curious, why is this bad? I personally hate the fact, that the above
doesn't compile for explicit operator bool(), which leads to idioms like
!!t.

Regards,
Kris

Which is causing multiprecision to misbehave when used in conjunction with
> uBlas: https://svn.boost.org/trac/**boost/ticket/8292#comment:2>
>
> I guess making the conversion explicit in C++11 mode would help (I still
> need to test that), but is there a C++03 solution?
>
> Thanks, John.
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk