Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 546801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:54:19+00:00 2026-05-13T10:54:19+00:00

Trying to find a simple to use safe_bool idiom/implementation, I’ve ended up with my

  • 0

Trying to find a “simple to use” safe_bool idiom/implementation, I’ve ended up with my own.

Q: Is this implementation correct?

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

private:
   void safe_bool_true() const {}

private:
   bool operator ==(safe_bool<T> const & rhs);
   bool operator !=(safe_bool<T> const & rhs);
};

to be used like this:

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

The only addition to existing base classes would be to_bool_type, but I hope I’ve got everything else correct, too.

The test cases I used (VC9) can be found here.

The downsides I see in implementation: bool_type and to_bool_type are visible in derived classes, which might not appease to everyone. Also, using the wrong template argument (e.g. class B : public safe_bool<A> introduced during copy-and-paste) will go unnoticed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:54:19+00:00Added an answer on May 13, 2026 at 10:54 am

    Using a pointer to a member function as the bool alias is idiomatic, as you’re doing here.

    Your implementation looks correct for what is there, but slightly incomplete. See http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool

    IMO safe_bool falls into the category of things which do more harm than good; ie the complexity and confusion introduced by this idiom along with the mental effort needed to understand it are greater than the intial problem it is intending to solve.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to find some simple SQL Server PIVOT examples. Most of the examples that
I'm trying to find a simple way to change the colour of the text
I'm trying to find a simple MySQL statement for the following two problems: I
What I want to do is very simple but I'm trying to find the
Trying to find an XML file I can use in lieu of a look-up
Been trying to find a working implementation of a WPF listview (or listbox) where
still trying to find where i would use the yield keyword in a real
I am trying to find a simple way to have a div with just
i am trying to make a very simple bash script to find files matching
Trying to find the sqlserver adapter for rails on windows. I have tried getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.