Imperfect C++ Practical Solutions for Real-Life Programming By Matthew Wilson | |
Table of Contents | |
Chapter 24. operator bool() |
24.4. operator !()—not!Another strategy I've seen[1] espoused is to supply only logical negation via operator !() const. The corresponding idiom is to double negate to test for truth, as in:
Listing 24.3.class ExpressibleThing { public: bool operator !() const; }; ExpressibleThing e; if(!!e) // Evaluates whether e "is" {} if(!e) // Evaluates whether e "is not" {} This is just too nasty to be taken seriously. Imagine the confusion on even moderately complex conditional expressions, and not just for novice programmers. The opportunities for errors to creep in during maintenance are too great. Last, it does not follow the normal look of code, which can only detract from its readability. |