Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2003-11-28 21:07:06


On 11/27/03 2:59 AM, "Joel de Guzman" <joel_at_[hidden]> wrote:

> Joel de Guzman <joel_at_[hidden]> wrote:
>> Daryle Walker <darylew_at_[hidden]> wrote:
>>> On 11/24/03 10:47 PM, "Joel de Guzman" <joel_at_[hidden]> wrote:
>>
>>> BTW, Spirit does have a solution for this problem, right? Otherwise, you
>>> have a major bug!
>>>
>>> (Note that no matter how you define Boolean-conversion, you need to
>>> explicitly provide operators == and != for your type, "poisoned" if such
>>> objects aren't supposed to be comparable, and conventional if they are.)
>>
>> You must've missed this:
>>
>> 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?
>
> Some tweaks:
>
> template <typename T>
> struct safe_bool
> {
> T* dummy;
> typedef T* safe_bool::*type;
> };
>
> template <typename T>
> inline typename safe_bool<T>::type
> make_safe_bool(bool cond)
> {
> return cond ? &safe_bool<T>::dummy : 0;
> }
>
> Here's a sample:
>
> operator safe_bool<match<nil_t> >::type() const
> {
> return make_safe_bool<match<nil_t> >(len >= 0);
> }

I'm assuming that this operator is in the class definition of
"match<nil_t>", right? And that all your other classes use "safe_bool", but
substitute themselves for the "T" parameter here, right? If you do that,
then you shouldn't have to worry about objects of different Spirit types
comparing with each other. (As I said in my note above, objects of the same
Spirit type still indirectly get operators == and != because they have an
one-step conversion to the same built-in singular type. If you haven't
already, you need to add those operators to your Spirit types, and either
define them properly or "poison" them private.)

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT hotmail DOT com

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