Previous section   Next section

Imperfect C++ Practical Solutions for Real-Life Programming
By Matthew Wilson
Table of Contents
Chapter 24.  operator bool()


24.6. operator int boolean::*() const

The operator boolean const *() const technique has been my preferred way of doing things until the last few years, but most compilers have now improved to the point where they support a much more satisfying method [Vand2003] proposed by Peter Dimov on the Boost newsgroups. The problem with any of the pointer-based techniques is that they allow conversion to void (const)*, which means that a type providing this operator can undergo unwanted conversions. However, there is a class of pointer types—pointers to members—that cannot be converted, either implicitly or via casts, to a regular pointer type. This results in the following operator:

Listing 24.4.


class ExpressibleThing


{


private:


  struct boolean { int i; };


public:


  operator int boolean ::*() const


  {


    return <condition> ? &boolean::i : NULL;


  }


};



This is great stuff, since it has all the attributes we require of an operator "Boolean".


      Previous section   Next section