Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy, overloaded C++ && operator?

I'm trying to implement my own boolean class, but cannot replicate native semantics for &&. The following contrived code demonstrates the issue:



    #include <iostream>>

    class MyBool {
    public:
        bool theValue;
        MyBool() {}
        MyBool(bool aBool) {theValue = aBool;}
        MyBool operator&& (MyBool aBool) {return theValue && aBool.theValue;}
    };

    bool f1() {std::cout << "   First\n"; return false;}
    bool f2() {std::cout << "   Second\n"; return false;}

    int main(int argc, char** argv) {
        std::cout << "Native &&\n";
        f1() && f2();
        std::cout << "Overloaded &&\n";
        MyBool(f1()) && MyBool(f2());
        return 0;
}

When compiled and run, the result is:


    Native &&
       First
    Overloaded &&
       Second
       First

In other words, && on bools is lazy (as any C++ programmer would expect) but the overloaded && isn't (as this C++ programmer at least didn't expect).

Is there a way to make overloaded && lazy? I can find various full-on lazy evaluation schemes to provide Haskell-like functionality, but they seem like complete overkill for my use case.

like image 245
user2285449 Avatar asked Apr 16 '13 08:04

user2285449


3 Answers

You should not overload bool operator&&, since you lose short circuit evaluation, as you have discovered.

The correct approach would be to give your class a bool conversion operator

class MyBool {
 public:
  bool theValue;
  MyBool() {}
  MyBool(bool aBool) : theValue(aBool) {}
  explicit operator bool() { return theValue; }
};

Note that explicit conversion operators require C++11 compliance. If you do not have this, have a look at the safe bool idiom.

like image 59
juanchopanza Avatar answered Oct 16 '22 20:10

juanchopanza


Is there a way to make overloaded && lazy?

No.

like image 39
Tony Delroy Avatar answered Oct 16 '22 20:10

Tony Delroy


You can make almost anything evaluate lazily with the expression template idiom, including but not limited to the operators whose built-in versions short-circuit. But that's more work than you need for this one case, since then your MyBool class would require a lot more code.

like image 2
Steve Jessop Avatar answered Oct 16 '22 21:10

Steve Jessop