views:

209

answers:

4

Have you ever seen any library/code that overloaded boolean operators, which is said to be evil? and What advantages does it give to the user?

+2  A: 

I don't know if anyone has ever done it, but || is used by ORACLE SQL as a string concatenation. See here:

http://www.java2s.com/Code/Oracle/Char-Functions/StringStringconcatenatestwostrings.htm

So, if you were trying to make a library that mimicked Oracle SQL in C++ and had a SQLString class, I guess using || for concatenation would be considered normal.

Lou Franco
Actualy, the || operator is the ANSI SQL string concat operator - it's not specific to ORACLE.
anon
ahh -- corrected
Lou Franco
+3  A: 

The standard library itself overloads operator ! for input streams, so perhaps "evil" is a touch strong?

But I suspect that you were talking about && and ||. The reason for not overlaoding these is that their short-circuting abilities cannot be duplicated in the user defined overloads, and no I am not aware of any library that overloads them.

anon
+1  A: 

Overloading the boolean operators is useful for exactly that - when you want your type to be able to behave like a boolean.
like any other language feature it has it's advantages as well as its perils.

shoosh
Brian Neal
+1  A: 

nice article which described why should be carefull with operator bool
http://www.artima.com/cppsource/safebool.html

boost have helpers for operator overloading

you should be logical carefull when overloading this operators. e.g. something::operator != should be same as ! something::operator ==

bb