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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:44:52+00:00 2026-06-09T11:44:52+00:00

Does the boost library provide an implementation of a safe bool idiom, so that

  • 0

Does the boost library provide an implementation of a safe bool idiom, so that I could derive my class from it?

If yes – where is it?

If no – what are my alternatives beyond implementing it myself?


I found the following similar question: ” Is there a safe bool idiom helper in boost? ” and the accepted answer suggests using bool_testable<> in Boost.Operators.

Unfortunately, when I checked the boost manual I couldn’t find it there. Code using it fails to compile too.

I also stumbled on another SO question ” Was boost::bool_testable<> relocated or removed? ” and the comment there suggests that the bool_testable actually never made to any release version of the boost.

There is also an interesting article by Bjorn Karlsson on the subject which contains a code which could be copy-pasted into my project. I am hoping however, that there is a commonly accepted and maintained utility library (e.g. boost) that implements that already.


For compatibility reasons, I do not want to rely on C++11.

  • 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-09T11:44:53+00:00Added an answer on June 9, 2026 at 11:44 am

    I do not know of a commonly accepted utility library that provides the safe-bool idiom. There have been a few attempts within Boost, and they often result in debates about how to provide a safe-bool implementation (naming conventions, macros, inline includes, inheritance). As a result, there are at least three implementations existing within Boost, with only one of the implementations, Boost.Spirit.Classic’s safe_bool, designed for external use.


    Details and concepts for each implementation:

    • Boost.Range’s safe_bool
      • Contained within the detail directory, so not explicitly designed for external use.
      • Implemented by using a template helper type and static member functions.
      • The safe-bool enabled class is expected to:
        • Provide an operator boost::range_detail::safe_bool< MemberPtr >::unspecified_bool_type() const member function that delegates to the static safe_bool::to_unspecified_bool() function.
    • Boost.SmartPtr’s operator_bool:
      • Contained within the detail directory, so not explicitly designed for external use.
      • The header file is intended to be included directly within a class definition. See shared_ptr.hpp for an example.
      • Requires including boost/detail/workaround.hpp before including smart_ptr/detail/operator.hpp.
      • The surrounding safe-bool enabled class is expected to:
        • Provide a this_type type.
        • Provide a T type.
        • Provide a T* px member variable.
    • Boost.Spirit.Classic’s safe_bool
      • Designed for external use.
      • Uses the CRTP pattern.
      • Designed to support base class chaining, allowing boost::spirit::class::safe_bool to be used without mandating multiple inheritance on the derived class.
      • The safe-bool enabled class is expected to:
        • Publicly derive from boost::spirit::classic::safe_bool< Derived >. If Derived already inherits from Base, then use boost::spirit::classic::safe_bool< Derived, Base >.
        • Provide a bool operator_bool() const member function.

    This example uses Boost 1.50. Each class should evaluate to true in boolean context if the integer passed to the constructor is greater than 0:

    // Safe-bool idiom with Boost.Range.
    #include <boost/range/detail/safe_bool.hpp>
    class range_bool
    {
    public:
      range_bool( int x ) : x_( x ) {}
    private:
      // None of these are required, but makes the implementation cleaner.
      typedef boost::range_detail::safe_bool< int range_bool::* > safe_bool_t;
      typedef safe_bool_t::unspecified_bool_type unspecified_bool_type;
      int dummy;
    public:
      operator unspecified_bool_type() const
      {
        return safe_bool_t::to_unspecified_bool( x_ > 0, &range_bool::dummy );
      }
    private:
      int x_;
    };
    
    // Safe-bool idiom with Boost.SmartPtr.
    #include <boost/detail/workaround.hpp>
    class smart_ptr_bool
    {
    public:
      smart_ptr_bool( int x ) { px = ( x > 0 ) ? &dummy : 0 ; }
    private:
      typedef smart_ptr_bool this_type; // -.
      typedef int T;                    //   :- Required concepts when using
      T* px;                            // -'   smart_ptr's operator_bool.
    private:
      T dummy; // Simple helper.
    public:
      #include <boost/smart_ptr/detail/operator_bool.hpp>
    };
    
    // Safe-bool idiom with Boost.Spirit.
    #include <boost/spirit/include/classic_safe_bool.hpp>
    class spirit_bool: public boost::spirit::classic::safe_bool< spirit_bool >
    {
    public:
      spirit_bool( int x ) : x_( x ) {} 
    public:
      // bool operator_bool() is required by the spirit's safe_bool CRTP.
      bool operator_bool() const { return x_ > 0; }
    private:
      int x_;
    };
    
    #include <iostream>
    
    int main()
    {
      std::cout << "range_bool( -1 ):     " << range_bool( -1 )     << std::endl
                << "range_bool(  1 ):     " << range_bool(  1 )     << std::endl
                << "smart_ptr_bool( -1 ): " << smart_ptr_bool( -1 ) << std::endl
                << "smart_ptr_bool(  1 ): " << smart_ptr_bool(  1 ) << std::endl
                << "spirit_bool( -1 ):    " << spirit_bool( -1 )    << std::endl
                << "spirit_bool(  1 ):    " << spirit_bool(  1 )    << std::endl;
      return 0;
    }
    

    Resulting output:

    range_bool( -1 ):     0
    range_bool(  1 ):     1
    smart_ptr_bool( -1 ): 0
    smart_ptr_bool(  1 ): 1
    spirit_bool( -1 ):    0
    spirit_bool(  1 ):    1

    I do not know of any alternatives. When I have ran across safe-bool idioms, most of the implementations have been a copy-and-paste variants of the implementation provided in Bjorn Karlsson’s article.

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

Sidebar

Related Questions

What is the purpose of using the Boost.MPI library? Does it increase performance? And
How does one use boost::spirit with an input that consists of something other than
The following does not compile: class Foo { public: Foo( boost::shared_ptr< Bar > arg
In C++, better without Boost library, how to make sure that the std::string str
Could someone let me know whether boost thread library leaks. It seems to me
I have a C++ library with a particular function that returns a boost::any ,
Does C++ standard library and/or Boost have anything similar to the filter function found
Why does nobody seem to use tuples in C++, either the Boost Tuple Library
I am working on a client-server application that uses boost::serialization library for it's serialization
I've looked at both the Named Parameter Idiom and the Boost::Parameter library . What

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.