
AMDG Vladimir.Batov@wrsa.com.au wrote:
Repeating safe-bool scaffolding over and over again is a real nuisance. It is worse for the end users as not exactly everyone is intimately familiar with the issue and the safe-bool workaround. Can we generalize it and put something like the following into utilities?
struct safebool { typedef void (safebool::*unspecified_bool_type)() const; typedef unspecified_bool_type result;
explicit safebool(bool v) : value_(v) {}
operator result() const { return value_ ? &safebool::internal_bool : 0; }
private:
void internal_bool() const {};
bool value_; };
That way all we (and the users) will have to do to deploy the technique will be
class Foo { ... operator safebool::result() const { return safebool(my_condition); } };
This seems simple and hassle-free.
I just noticed this in boost/detail/identifier.hpp typedef void (*unspecified_bool_type)(D); // without the D, unspecified_bool_type static void unspecified_bool_true(D){} // conversion allows relational operators // between different identifier types So, here's another take on it: template<class Tag> struct safe_bool { typedef void (safe_bool::*result_type)(); result_type operator()(bool b) { return b? &safe_bool::true_ : 0 } private: void true_() {} }; In Christ, Steven Watanabe