c++ - Safe Bool Idiom - did I find a simpler way, or did I miss something? -


i needed find safe bool idiom solution. know drill, i'm using vs 2012, xcode, gcc, llvm, various platforms, support c++11 explicit conversion operator , don't. needed portable.

i found several, devised seems simple have ask, missing here?

background (brief):

several examples 1 web provide either highly intrusive methods (essentially putting required inside given class), or generic approach using crtp, based on pointer member function (which of wildly unpredictable size , performance penalty).

then read boost::spirit::classic safe bool, tried it, examined it, etc. liked spirit's implementation depended upon pointer member data rather pointer member function (with compiler workaround 'obscure' versions).

so thought, doesn't matter type pointer member based upon, implementations working because, among other things, these pointers members of other objects we're use.

so, tried kind of thing (and i'm wondering if i'm way off or ok this)

first:

class safebool  { private:  safebool() : value( 0 ) { }   public:   int value; }; 

safebool takes job of boost::spirit's safe_bool template without imposing burden of driving user classes it.

// pointer member typedef, made available  typedef int safebool :: *  safe_bool;  //  more why define in moment #define explicit_operator_bool operator safe_bool   // here example user class, smart pointer or  template< typename t > class somesptr  {  public:  ... other class stuff,   explicit_operator_bool() const       { if ( get() ) return &safebool::value;           return 0;        } }; 

first, explicit_operator_bool define can switched on c++11 compliant compilers use "real" explicit operator bool, whereas on vs 2012 , old gcc (or have you), can here, conversion operator safe_bool.

one can use related define return...say

safe_bool_return( get() ) 

which modulate return logic make code switchable between modern , older compilers (that is, c++11 define resolve return get() != 0)

now, had same basic results on vs 2012 spirit's code. allowed

someptr< atype > p, q; // or such  if ( p ) { } // works expected 

while @ same time denying code like

if ( p < q ) // compile time error if ( p > q ) // "" 

but allowed this:

if ( p == q ) // compiles fine 

this same result number of implementations, several like:

template< typename t > bool operator ==( const safe_bool & , const t & )  {   safebool cantcomparethattype;   return false;  }  template< typename t > bool operator ==( const t &, const safe_bool & )  {   safebool cantcomparethattype;   return false;  } 

or similar trickery, template code doesn't instantiate until operator used, , since safebool constructor private, generates error if (p == q) attempted.

obviously other operators (!=....), etc excluded handled required (template functions respond whatever type has explicit_operator_bool created function in it).

ok, here's question. deluding myself here? other implementations impose burden of deriving template class , placing bool function in user's class - or 1 puts whole thing user class.

it appears me approach uses 1 class, safebool (which never intended instantiation) provide pointer member, , type (the class itself) upon safe bool idiom can implemented while imposing single function in user's class, mimics c++11 explicit conversion operator, possibly enabling more upwardly portable code idiom needed.

please, stop me if i'm wrong this!

your technique works, , discussed 1 of http://www.artima.com/cppsource/safebool.html . discusses option works. called "poisoning operators" , douglas gregor credited inventing it.

i think real safe_bool idiom got more headway being more safe. in particular, if safe_bool of type int t::*, , pointer integer member of t private, class can generate particular variant of safe_bool, easier prove no 1 can generate unsafe patterns. since can take address of &safebool::value, "less safe."

pathological? perhaps.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -