C++ question: What is the need for ' operator type_safe_bool_type () '

Apr 1, 2013 at 6:21 AM
I commented out :
typedef bool (opt::*type_safe_bool_type) () const;
        CPPLINQ_INLINEMETHOD operator type_safe_bool_type () const throw ()
        {
            return is_initialized ? &opt::has_value : nullptr;
        }
And the test sample I wrote compiles.
Apr 1, 2013 at 10:36 AM
Check out http://www.artima.com/cppsource/safebool2.html for an explanation of how it works
Coordinator
Jun 26, 2013 at 7:56 PM
Hi there.

First of let me apologize for the long delay before answering your question.

The purpose of the operator is to be able to safely write:
if (obj) 
{ 
    // Do something
}
The pattern was inspired by the boost code where I first saw this technique.

Perhaps the safebool approach is a better approach. If you do a pull-request I can take a look at the code.

Regards,

Mårten