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 6651013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:57:37+00:00 2026-05-26T00:57:37+00:00

I was pointed to the ‘safe bool idiom’, and after trying to decipher what

  • 0

I was pointed to the ‘safe bool idiom’, and after trying to decipher what is going on (the explanation supplied on the site was not sufficient enough to grant me understanding of why it works), I decided to try to take the following code apart and make an attempt at simplifying it as much as possible. The site supplied code below:

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;
    }
  };

I decided to analyse the key basis of ‘bool_type’ given this seems to be what it’s centred on. Given the following line:

typedef void (Testable::*bool_type)() const;

One can (not so easily, due to bracketing) deduce it’s a typedef of a type of ‘void Testable::*’, of which bool_type represents. This can be further demonstrated by making the following modifications and function calls:

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) {}

    bool_type Test; //Added this

    operator bool_type() const {
      return ok_==true ?
        &Testable::this_type_does_not_support_comparisons : 0;
    }
  };

int main()
{
    Testable Test;
    int A = Test.Test; //Compiler will give a conversion error, telling us what type .Test is in the process
}

It allows us to see what type bool_type is:

error: cannot convert ‘void (Testable::*)()const’ to ‘int’ in initialization

Which shows it is indeed a type of ‘void (Testable::*)’.

The issues crops up here:

If we modify the following function:

    operator bool_type() const {
      return ok_==true ? 
        &Testable::this_type_does_not_support_comparisons : 0;
    }

And turn it into:

    operator void Testable::* () const //Same as bool_type, right? 
    {
      return ok_==true ? 
        &Testable::this_type_does_not_support_comparisons : 0;
    }

It generates the following complaints:

error: expected identifier before ‘*’ token
error: ‘< invalid operator >’ declared as function returning a function

My questions are thus:

Why is it generating those complaints if ‘void (Testable::*) is indeed the typedef for bool_type?

And

What is going on here?

  • 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-26T00:57:37+00:00Added an answer on May 26, 2026 at 12:57 am

    Your reasoning goes wrong about here

    operator void Testable::* () const //Same as bool_type, right? 
    

    This isn’t correct. The type of bool_type is, as the compiler tells us in the error message:

    ‘void (Testable::*)()const’

    So, to replace it in the operator, you would need something like

    operator (void (Testable::*)() const) () const
    

    if that is ever possible! See why even the ugly typedef is an improvement?

    In C++11 we also have the new construct explicit operator bool() to save us from this ugliness.

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

Sidebar

Related Questions

After being pointed to using glBitmap() and this very good tutorial/explanation of its usage
Edit--@Uri correctly pointed out that this was an abuse of annotations; trying to actually
Lately I was pointed to http://opencpu.org/ . Nifty website, but after browsing for a
Hi I'm trying to reuse some code I was pointed to earlier to run
It was pointed out to me that the statement below is not recursion. I
@Jonah pointed me to the following explanation he has provided: Question on View Controller's
As pointed out in the question: EmptyDataTemplate and EmptyDataText not working in GridView using
After being pointed in the right direction to learn about views and layouts I
A colleague pointed me to a strange case in C# (not so sure if
Im looking to be pointed in the right direction. My site that i'm testign

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.