Boost logo

Boost :

Subject: Re: [boost] Boost Optional
From: Rafael Justo (cadastros_at_[hidden])
Date: 2012-03-29 09:05:14


Hello!

In my example I just used an "else if" statement to test the two cases of
the patch that I suggested (both false or both true). Just to make it
clear, I don't write this kind of code, It's an illustration of the problem
cases.

In the company that I work we don't usually use "!" because is to discreet.
But now I realized that when you have a NULL object you can just check if
it's not null with "!". And I agree with Stewart, this new operator can be
confusing for the programmer. The programmer can make a comparison thinking
that he is comparing the content of the boost::optional object.

Thank you guys for all answers.

Best regards,
Rafael Justo

On Thu, Mar 29, 2012 at 8:21 AM, Stewart, Robert <Robert.Stewart_at_[hidden]>wrote:

> Rafael Justo wrote:
> >
> [I trimmed your example:]
> > boost::optional<Test> get();
> >
> > int main()
> > {
> > if (get() == true) {
> > // OK
> > } else if (get() == false) {
> > // ERROR
> > }
> > }
> >
> > But there's no "operator==" with bool in optional.hpp,
> > so the above code don't compile (i have checked version
> > 1.49).
>
> I agree with the other posters that such verbosity is unnecessary.
>
> > Is it possible to add the patch described bellow in
> > optional.hpp to solve this problem?
> >
> > + bool operator== (const bool condition)
> > + {
> > + return ((*this && condition) || (!*this &&
> > !condition));
> > + }
> > +
>
> That would make optionals equality comparable with anything that can be
> converted to bool, so the following would be valid:
>
> if (get() == 3) // int is convertible to bool
> {
> // OK?
> }
>
> Comparing boost::optional<Test> with int or anything else convertible to
> bool is inappropriate and was the reason to introduce the Safe Bool Idiom
> in the first place.
>
> _____
> Rob Stewart robert.stewart_at_[hidden]
> Software Engineer using std::disclaimer;
> Dev Tools & Components
> Susquehanna International Group, LLP http://www.sig.com
>
>
>
>
> ________________________________
>
> IMPORTANT: The information contained in this email and/or its attachments
> is confidential. If you are not the intended recipient, please notify the
> sender immediately by reply and immediately delete this message and all its
> attachments. Any review, use, reproduction, disclosure or dissemination of
> this message or any attachment by an unintended recipient is strictly
> prohibited. Neither this message nor any attachment is intended as or
> should be construed as an offer, solicitation or recommendation to buy or
> sell any security or other financial instrument. Neither the sender, his or
> her employer nor any of their respective affiliates makes any warranties as
> to the completeness or accuracy of any of the information contained herein
> or that this message or any of its attachments is free of viruses.
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


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