
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

On Wed, Mar 28, 2012 at 8:55 PM, Rafael Justo <cadastros@rafael.net.br>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.

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@sig.com 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.

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@sig.com>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@sig.com 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
participants (4)
-
Laurent Dupuy
-
Rafael Justo
-
Robert Jones
-
Stewart, Robert