Boost logo

Boost :

Subject: Re: [boost] Boost Optional
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2012-03-29 07:21:15

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


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.

Boost list run by bdawes at, gregod at, cpdaniel at, john at