Boost logo

Boost :

Subject: [boost] safe bool not safe enough ?
From: John Maddock (boost.regex_at_[hidden])
Date: 2013-03-14 07:50:02


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!

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