Boost logo

Boost :

Subject: Re: [boost] Boost Optional
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2012-03-29 04:18:41


On Wed, Mar 28, 2012 at 8:55 PM, Rafael Justo <cadastros_at_[hidden]>wrote:

> Hello! This is my first post in this list. =)
>
> I was looking today at Boost Optional (great work!).
> And I was wondering how could I do something like the bellow code.
>
> --------------------------------
> #include <boost/optional.hpp>
>
> class Test {};
>
> boost::optional<Test> get()
> {
> return boost::optional<Test>();
> }
>
> int main()
> {
> if (get() == true) {
> // OK
>
> } else if (get() == false) {
> // ERROR
> }
>
> return 0;
> }
> --------------------------------
>
> But there's no "operator==" with bool in optional.hpp,
> so the above code don't compile (i have checked version 1.49).
> Is it possible to add the patch described bellow in optional.hpp
> to solve this problem?
>
> --------------------------------
> --- boost_1_49_0/boost/optional/optional.hpp 2010-12-18
> 19:29:39.000000000 -0200
> +++ boost_1_49_0_new/boost/optional/optional.hpp 2012-03-28
> 16:03:53.417578997 -0300
> @@ -616,6 +616,11 @@
> return *this ;
> }
>
> + bool operator== (const bool condition)
> + {
> + return ((*this && condition) || (!*this && !condition));
> + }
> +
> void swap( optional & arg )
> {
> // allow for Koenig lookup
> --------------------------------
>
> Best regards,
> Rafael Justo
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>

Can you not simply say this?

int main()
{
 if (get()) {
   // OK

 } else if (!get()) {
   // ERROR
 }

 return 0;
}

- Rob.


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