Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2003-11-25 18:46:51


Peter Dimov <pdimov_at_[hidden]> wrote:
> Joel de Guzman wrote:
>>
>> IIUC, what you mean is that we do not have to review something like
>> that. Ok, but it would still be good to have a single implementation.
>> Spirit uses:
>>
>> struct safe_bool_impl
>> {
>> int* dummy;
>> };
>>
>> typedef int* safe_bool_impl::*safe_bool;
>>
>> inline safe_bool
>> make_safe_bool(bool cond)
>> {
>> return cond ? &safe_bool_impl::dummy : 0;
>> }
>>
>> and it is used all throughout.
>
> Then you should call it unsafe_bool since having a conversion to it permits
> comparisons between unrelated types.

You are right! How could I have missed that?

Second take:

    template <typename T>
    struct safe_bool
    {
        int* dummy;
        typedef int* safe_bool::*type;
    };

    template <typename T>
    inline typename safe_bool<T>::type
    make_safe_bool(bool cond)
    {
        return cond ? &safe_bool<T>::dummy : 0;
    }

Can you spot some problems with this too? The point is that it would be
nice to have one *correct* and generic implementation that everyone
can use. It's easy to get it wrong (I did). There are some subtle issues
that make it an ideal candidate for a small library component (e.g.
Borland does not like the pointer to member stuff, maybe there's a
better formulation for Borland? e.g. While the code above is fine with
g++3.2 and Comeau, VC7.1 does not like it in some cases, etc...).

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