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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:53:46+00:00 2026-05-13T08:53:46+00:00

I have recently read the safe bool idiom article. I had seen this technique

  • 0

I have recently read the safe bool idiom article. I had seen this technique used a few times, but had never understood quite why it works, or exactly why it was necessary (probably like many, I get the gist of it: simply using operator bool () const allowed some implicit type conversion shenanigans, but the details were for me always a bit hazy).

Having read this article, and then looked at a few of its implementations in boost’s shared_ptr.hpp, I thought I had a handle on it. But when I went to implement it for some of the classes that we’ve borrowed and extended or developed over time to help manage working with Windows APIs, I found that my naive implementation fails to work properly (the source compiles, but the usage generates a compile-time error of no valid conversion found).

Boost’s implementations are littered with conditions for various compilers level of support for C++. From using the naive operator bool () const, to using a pointer to member function, to using a pointer to member data. From what I gather, pointer to member data is the most efficient for compilers to handle IFF they handle it at all.

I’m using MS VS 2008 (MSVC++9). And below is a couple of implementations I’ve tried. Each of them results in Ambiguous user-defined-conversion or no operator found.

template<typename HandlePolicy>
class AutoHandleTemplate
{
public :
    typedef typename HandlePolicy::handle_t handle_t;
    typedef AutoHandleTemplate<HandlePolicy> this_type;
    {details omitted}
    handle_t get() const { return m_handle; }
    operator handle_t () const { return m_handle; }

#if defined(NAIVE)
    // The naive implementation does compile (and run) successfully    
    operator bool () const { return m_handle != HandlePolicy::InvalidHandleValue(); }
    bool operator ! () const { return m_handle == HandlePolicy::InvalidHandleValue(); }
#elif defined(FUNC_PTR)    
    // handle intrinsic conversion to testable bool using unspecified_bool technique
    typedef handle_t (this_type::*unspecified_bool_type)() const;
    operator unspecified_bool_type() const // never throws
    {
        return m_handle != HandlePolicy::InvalidHandleValue() ? &this_type::get() : NULL;
    }
#elif defined(DATA_PTR)

    typedef handle_t this_type::*unspecified_bool_type;
    operator unspecified_bool_type() const // never throws
    {
        return m_handle != HandlePolicy::InvalidHandleValue() ? &this_type::m_handle : NULL;
    }
#endif
private :
    handle_t m_handle;
{details omitted}
};

And here’s a snippet of code that either works (naive implementation), or errors (either of the unspecified_bool techniques, above):

// hModule is an AutoHandleTemplate<ModuleHandlePolicy>
if (!hModule)

and:

if (hModule)

I have already tried enabling the operator! in all cases – but although the first case then works, the second fails to compile (ambiguous).

This class seems to me to be so very like a smart_ptr (or auto_ptr). It should support implicit conversion to its underlying handle type (HMODULE) in this case, but it should also handle if (instance) and if (!instance). But if I define both the operator handle_t and the unspecified_bool technique, I get errors.

Can someone please explain to me why that is so, and perhaps suggest a better approach? (or should I be content with the naive approach, at least until C++0x is complete and explicit operators are implemented in my compiler)?

EDIT:

It seems that the answer may well be that if I define an implicit conversion to an integral, that C++ will use that conversion for any if (instance) type expressions. And that, at least for the above class, the only reason to define any other operators (operator bool) is to explicitly override using the implicit integral conversion to something else (in the above case, forcing it to be a comparison to INVALID_HANDLE_VALUE instead of the implicit NULL).

And using the unspecified_bool technique only really makes sense when you’re not providing an integral conversion operator?

  • 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-13T08:53:47+00:00Added an answer on May 13, 2026 at 8:53 am
    AutoHandleTemplate<ModuleHandlePolicy> hModule( ... );
    HMODULE raw_handle = hModule; // if we want to this line works,
    // AutoHandleTemplate<ModuleHandlePolicy> should \
    //    be implicitly converted to it's raw handle type - HMODULE.
    

    If one smart-ptr can implicitly converted to it’s raw handle type and the raw handle type could be used in a boolean test itself, like :

    HMODULE the_raw_handle = ...;
    if ( the_raw_handle ) {}  // this line is ok
    

    For those smart-ptrs, there is no need (and should not) to define conversions to bool,void* or safe_bool, otherwise, ambiguity.

    operator bool(), void*(), safe_bool() are used for the smart-ptrs which could not be implicitly convert to it’s raw handle or it’s raw handle couldn’t used in a boolean context.

    Try this code :

    template<typename HandlePolicy>
    class AutoHandleTemplate
    {
    public :
          typedef typename HandlePolicy::handle_t handle_t;
          typedef AutoHandleTemplate<HandlePolicy> this_type;
          {details omitted}
    
          operator handle_t () const {
                return m_handle==HandlePolicy::InvalidHandleValue()? 0: m_handle;
          }
    
          // no more conversion functions
    
    private :
          handle_t m_handle;
          {details omitted}
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 275k
  • Answers 275k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Is there a reason why you can't use Heroku as… May 13, 2026 at 2:41 pm
  • Editorial Team
    Editorial Team added an answer hi Sairam this was useful for me! step by step… May 13, 2026 at 2:40 pm
  • Editorial Team
    Editorial Team added an answer I would probably use composition and have a Worker who… May 13, 2026 at 2:40 pm

Related Questions

I have been watching the growing visibility of functional programming languages and features for
I've read a lot recently about how writing multi-threaded apps is a huge pain
For classes that have a long list of setters that are used frequently, I
I have recently read about how cursors should be avoided. Well, I would like
I recently read the IXmlSerializable interface information on MSDN and was a little confused

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.