Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2003-11-27 19:01:31


Douglas Paul Gregor <gregod_at_[hidden]> wrote:

> Is Peter's idiom _really_ that hard to implement?

Here's a safe_bool code from shared_ptr:

#if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)

    operator bool () const
    {
        return px != 0;
    }

#elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
    typedef T * (this_type::*unspecified_bool_type)() const;
    
    operator unspecified_bool_type() const // never throws
    {
        return px == 0? 0: &this_type::get;
    }

#else

    typedef T * this_type::*unspecified_bool_type;

    operator unspecified_bool_type() const // never throws
    {
        return px == 0? 0: &this_type::px;
    }

#endif

    // operator! is redundant, but some compilers need it

    bool operator! () const // never throws
    {
        return px == 0;
    }

All these gunk can be hidden in a safe_bool library code. The Dimov/Vandevoorde
idiom can be encapsulated in a library. I've posted code, as did Dave Abrahams
and Nicola Musatti. Whichever interface/implementation we choose is prefectly
fine by me as long as we choose only one so we won't have to repeat such code as
above.

Regards,

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk