senf::safe_bool< T > Class Template Reference

Mixin class for safe boolean conversion support. More...

#include <senf/Utils/safe_bool.hh>

Inheritance diagram for senf::safe_bool< T >:

Additional Inherited Members

- Public Member Functions inherited from senf::comparable_safe_bool< T >
 operator bool_type () const
 
bool operator! () const
 
- Protected Types inherited from senf::safe_bool_base
typedef void(safe_bool_base::* bool_type) () const
 
- Protected Member Functions inherited from senf::comparable_safe_bool< T >
 ~comparable_safe_bool ()
 
- Protected Member Functions inherited from senf::safe_bool_base
void this_type_does_not_support_comparisons () const
 
 safe_bool_base ()
 
 safe_bool_base (const safe_bool_base &)
 
safe_bool_baseoperator= (const safe_bool_base &)
 
 ~safe_bool_base ()
 

Detailed Description

template<typename T>
class senf::safe_bool< T >

Mixin class for safe boolean conversion support.

This is a direct yet simplified copy of a safe bool solution by Bjorn Karlsson from http://www.artima.com/cppsource/safebool.html

This mixin provides the client class with safe boolean testing. It is a safe replacement for operator bool. operator bool is problematic since bool is an integer type. This conversion operator makes the class usable in any numeric context, which can be quite dangerous. The operator void * solution is much better in this respect but still allows two instances of any class having such a void * conversion to be compared for equality. This again will produce absolutely unexpected results since it will not check whether the objects are identical, it will only check, that both return the same boolean state.

This solutions solves all these problems by returning a pointer-to-member which cannot be converted to any other type. By providing explicit implementations of operator== and operator!= which fail in an obvious way at compile time, this hazard is removed.

To make a class boolean testable, just inherit from the mixin and implement boolean_test:

class Testable
: public safe_bool<Testable>
{
public:
bool boolean_test() const
{
// Perform Boolean logic here
}
};
Testable t = ...;
if (t) {
...
}
    If the class to be made using senf::safe_bool is itself comparable via it's own \c
    operator==, you must use comparable_safe_bool instead of safe_bool to not loose this
    capability.

    \see comparable_safe_bool

Definition at line 122 of file safe_bool.hh.


The documentation for this class was generated from the following file: