Click here to Skip to main content
11,719,973 members (82,326 online)
Click here to Skip to main content

Tagged as

Reusable safe_bool Implementation

, 12 Jan 2010 CPOL 6.2K 3
Rate this:
Please Sign up or sign in to vote.
Reusable safe_bool implementation
safe_bool - what & why[^]?

In my understanding their implementation is broken (if fixable), but in the course I came up with my own that appears slightly more elegant to me:

template <typename T>
class safe_bool 
   typedef void (safe_bool::*bool_type)() const;
   bool_type to_bool_type(bool b) const 
    { return b ? &safe_bool<T>::safe_bool_true : 0; }
   void safe_bool_true() const {}
   bool operator ==(safe_bool<T> const & rhs); 
   bool operator !=(safe_bool<T> const & rhs); 

Can be used like this:

struct A : public safe_bool<A>
   operator bool_type() const { return to_bool_type(true); }
   // replaces:
   // operator bool() const { return true; }

From all samples I found that's the cleanest to write and read, while only the two symbols bool_type and to_bool_type "escape" to derived classes.

I am putting this up for review, since I'm clearly at the limits of my understanding of the standard. However, the following test cases pass on VC 2008:

struct A : public safe_bool<A>
   operator bool_type() const { return to_bool_type(true); }
struct B : protected safe_bool<B> // protected is ok, too
   operator bool_type() const { return to_bool_type(false); }
struct C : public B
   operator bool_type() const { return to_bool_type(rand() < RAND_MAX / 2); }

void SafeBootCompileFails()
   A a, a2;
   B b;
   C c;
   if (a) {}      // Yes!
   if (!a) {}     // Yes!

   a=a;           // Yes! (default assignment op should be allowed)
   a=a2;          // Yes! (default assignment op should be allowed)

   long l = a;    // No!

   if (a==a2) {}  // No!
   if (a!=a2) {}  // No!
   if (a<a2)  {}  // No!

   if (a==b) {}   // No!
   if (a!=b) {}   // No!
   if (a<b)  {}   // No!
   if (b==c) {}   // No!
   if (b!=c) {}   // No!
   if (b<c)  {}   // No!

   a=b;           // No!

   b = c;         // Yes, slicing. Not my problem today.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Germany Germany
Peter is tired of being called "Mr. Chen", even so certain individuals insist on it. No, he's not chinese.

Peter has seen lots of boxes you youngsters wouldn't even accept as calculators. He is proud of having visited the insides of a 16 Bit Machine.

In his spare time he ponders new ways of turning groceries into biohazards, or tries to coax South American officials to add some stamps to his passport.

Beyond these trivialities Peter works for Klippel[^], a small german company that wants to make mankind happier by selling them novel loudspeaker measurement equipment.

Where are you from?[^]

Please, if you are using one of my articles for anything, just leave me a comment. Seeing that this stuff is actually useful to someone is what keeps me posting and updating them.
Should you happen to not like it, tell me, too

You may also be interested in...

Comments and Discussions

GeneralKind of short on the why Pin
Tim Craig12-Jan-10 8:30
memberTim Craig12-Jan-10 8:30 
GeneralRe: Kind of short on the why Pin
peterchen12-Jan-10 9:49
memberpeterchen12-Jan-10 9:49 
Their sample (actually the best I could find in multiple attempts) seems to miss preventing comparison on the "non-virtual" case, and I can#t see any rationale for making CAD protected in safe_bool_base.

Also, I end up with one class (instead of two), don't need to muck around with pointers to member functions, and my implementaiton in the derived class is closer to the "simple" operator bool

Nothing really new, I've mainly put it up for peer review.

Personally, I love the idea that Raymond spends his nights posting bad regexs to mailing lists under the pseudonym of Jane Smith. He'd be like a super hero, only more nerdy and less useful. [Trevel]
| FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server

JokeRe: Kind of short on the why Pin
Jörgen Sigvardsson13-Jan-10 11:40
memberJörgen Sigvardsson13-Jan-10 11:40 
GeneralRe: Kind of short on the why Pin
peterchen13-Jan-10 12:24
memberpeterchen13-Jan-10 12:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 12 Jan 2010
Article Copyright 2010 by peterchen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid