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

  • SEARCH
  • Home
  • 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 3237042
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:41:30+00:00 2026-05-17T17:41:30+00:00

Is there a trick to get the safe bool idiom completely working without having

  • 0

Is there a trick to get the safe bool idiom completely working without having to derive from a class that does the actual implementation?

With ‘completely working’, I mean the class having an operator allowing it to be tested as if it were a boolean but in a safe way:

MyTestableClass a;
MyOtherTestableClass b;
  //this has to work
if( a );
if( b );
  //this must not compile
if( a == b );
if( a < b );
int i = a;
i += b;

when using this implementation for example

struct safe_bool_thing
{
  int b;
};
typedef int safe_bool_thing::* bool_type;
safe_bool( const bool b ) { return b ? &safe_bool_thing::b : 0; }

class MyTestableClass
{
  operator bool_type () const { return safe_bool( someCondition ); }
}

it’s almost fine, except a == b will still compile, since member pointers can be compared. The same implementation as above, but with pointer to member function instead of pointer to member variable has exactly the same problem.

Known implementations that do work perfectly (as described here for example, or the safe_bool used in boost) require that the testable class derive from a class providing the actual operator implementation.

I actually think there is no way around it, but’m not entirely sure. I tried something that looked a bit fischy but I thought it might work, yet is doesn’t compile at all. Why is the compiler not allowed to see that the operator returns a safe_bool_thing, which in turn can be converted to bool() and hence be tested?

struct safe_bool_thing
{
  explicit safe_bool_thing( const bool bVal ) : b( bVal ) {}
  operator bool () const { return b; }
private:
  const bool b;
  safe_bool_thing& operator = ( const safe_bool_thing& );
  bool operator == ( const safe_bool_thing& );
  bool operator != ( const safe_bool_thing& );
};

class MyTestableClass
{
  operator safe_bool_thing () const { return safe_bool_thing( someCondition ); }
};

MyTestableClass a;
if( a ); //conditional expression of type 'MyTestableClass' is illegal
  • 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-17T17:41:31+00:00Added an answer on May 17, 2026 at 5:41 pm

    This should work:

    class MyTestableClass
    {
    private:
      void non_comparable_type() {}
    public:
      typedef void (MyTestableClass::* bool_type)();
    
      operator bool_type () const { return (someCondition ? &MyTestableClass::non_comparable_type : 0); }
    };
    
    class MyOtherTestableClass
    {
    private:
      void non_comparable_type() {}
    public:
      typedef void (MyOtherTestableClass::* bool_type)();
    
      operator bool_type () const { return (someCondition ? &MyOtherTestableClass::non_comparable_type : 0); }
    };
    

    For blocking the if (a == b) case, it depends on the fact that both types convert to incompatible pointer types.

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

Sidebar

Related Questions

Is there some trick that I'm missing here? I've created a templated control, very
Is there any command line trick to get SVN to add in all the
Is there a special trick required to get BooleanElement's ValueChanged event firing? The code
Is there a trick to get the search results of GitHub as RSS feed?
Is there a trick in the Groove Web Services to find the size of
Is there some trick to getting a SpringMVC custom view to cause a file
Is there a trick to making 9patch images with single pixel borders? I'm not
So in Ruby there is a trick to specify infinity: 1.0/0 => Infinity I
Is there a simple command line switch or a trick to renaming the generated
Are there any tricks for preventing SQL Server from entitizing chars like &, <,

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.