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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:46:56+00:00 2026-06-07T06:46:56+00:00

I’ve seen a nice trick made by an boost Implementation, they somehow use the

  • 0

I’ve seen a nice trick made by an boost Implementation, they somehow use the overloading of the () operator to evaluate an instance of the class boost::system::error_code to an bool value

class error_code
{
...
typedef void (*unspecified_bool_type)();
static void unspecified_bool_true() {}

operator unspecified_bool_type() const  // true if error
{ 
  return m_val == 0 ? 0 : unspecified_bool_true;
}
...
}

This results in the possibility to check for an error like this

...
boost::system::error_code err

some_boost_func(err);
if(err)
{
    //handle error
}
....

So i keep asking myself.. what happend there?
This seems to somehow relate to the use of function pointers…
What happens if i call err does this evaluate the function itself or the function pointer?
But how can a void (*unspecified_bool_type)(); function return a value in

return m_val == 0 ? 0 : unspecified_bool_true;
  • 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-06-07T06:47:01+00:00Added an answer on June 7, 2026 at 6:47 am

    It really has little (or nothing) to do with core functionality of function pointers specifically. It is a trick that allows one to write a “safe” boolean-like conversion for the class.

    When one wants some class to be usable under if (and generally in logic contexts), one typically makes it convertible to bool. As in

    class Error {
    public:
      operator bool() const { /* whatever */ }
    };
    

    and now you can do

    Error err;
    ...
    if (err) // automatically intepreted as `if (err.operator bool())`
      ...
    

    However, since bool type is an integral type in C++, this might lead to undesirable consequences, when someone accidentally writes something like

    int i = err;
    

    or uses err in an arithmetic expression and it quietly compiles.

    For this reason, in many cases people prefer to introduce a conversion to pointer type, instead of conversion to bool, as in

    class Error {
    public:
      operator void *() const { 
        // Return null pointer for `false` and any non-null pointer for `true`
      }
    };
    

    This is better since one can use it under if, but one can’t make the previous mistake with int. I.e.

     if (err) // automatically interpreted as `if (err.operator void *() != 0)`
       ...
    

    will compile and work as intended, since the compiler will automatically convert err object to pointer type.

    However, such conversion will be automatically applied in pointer contexts as well (in addition to boolean contexts), meaning that one can still accidentally do

    void *p = err;
    

    or

    free(err);
    

    and it will quietly compile. This is also undesirable.

    To make it more difficult to accidentally misuse such error class, it is a good idea to use some more “exotic” pointer type, like pointer to a function. This is exactly what you see in the code you quoted. The unspecified_bool_type is used as a pointer-based pseudo-boolean type. Null value is returned for false and pointer to a dummy unspecified_bool_true function is returned for true. The function unspecified_bool_true is never called and never intended to be called. It only exists to reserve some unique pointer value to be used as true return.

    In some cases people take it one step further and use an even more “exotic” pointer type: pointer-to-class-member type. But for most applications pointer to function is “exotic” enough.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.