Thursday, April 14, 2011

Any library that overloaded boolean operators?

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?

From stackoverflow
  • 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.

    anon : Actualy, the || operator is the ANSI SQL string concat operator - it's not specific to ORACLE.
    Lou Franco : ahh -- corrected
  • 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.

  • 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.

    Brian Neal : The peril of overloading && and || is your version will not short circuit like the built-in operator versions. Thus many coding standards and style guides forbid overloading && and ||.
  • 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 ==

0 comments:

Post a Comment