2

Possible Duplicate:
Is there an Non-Short circuited logical “and” in C++?

I'm looking for a simple way to force the right side of a boolean expression (|| &&) to evaluate. Normally the right side is not evaluated if the left side can already determine the outcome.

Usually I revert to this syntax:

c = expr();
r = r || c;

since the following doesn't guarantee expr() will be evaluated:

r = r || expr();

Is there a shorter/simpler syntax to replace what I'm doing now? Or do I already have the most compact form?

Tagged as C and C++ since the solution might be shared. I actually code in C++

Community
  • 1
  • 1
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267

4 Answers4

2

There are many ways of doing this, firstly just evaluate both options and store the results as bools:

const bool a = expr();
const bool b = r;
return a || b;

Or you can use a function like so:

bool or(bool param1, bool param2)
{
    return param1 || param2;
}

and using the function will force both parameters to be evaluated before it is called.

Granted usually most people like the short circuit evaluation of boolean operators.

Dominik Grabiec
  • 10,315
  • 5
  • 39
  • 45
1

In Java a single pipe or ampersand will work for non short-circuit evaluation for boolean types, but in C as you don't have a boolean data type you could use the bitwise or/and operations (a single pipe or ampersand too), but you need to be extra careful because that won't work as desired if the operands are not limited to 0 and 1.

Example of misbehaviour when trying to replace logical with bitwise:

2 && 1 == 1
2 & 1  == 0

A solution that might work for you is to use some macros and take advantage of the neutral or reversible logical operations to enforce a "boolean" type conversion, then use the bitwise operations:

#define OR(A, B)  (!!(A) | !!(B))
#define AND(A, B) (!!(A) & !!(B))

About C++ I really don't know the details, but I guess the situation is the same as in C... Maybe you could do some operator overloading black magic serious shit xD.

fortran
  • 74,053
  • 25
  • 135
  • 175
0

No, what you are doing as best as it can get.

Asha
  • 11,002
  • 6
  • 44
  • 66
0

How about using the | or & operators? Works in VC++ for common scenerios.

#include <iostream>

bool A()
{
    std::cout<<"A";
    return true;
}

bool B()
{
    std::cout<<"B";
    return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
    if(!(B() & A()))
        std::cout<<"Passed one\r\n";
    if(A() | B())
        std::cout<<"Passed two\r\n";
    return 0;
}
J Trana
  • 2,150
  • 2
  • 20
  • 32
  • As pointed out @lasmana, this is indeed a duplicate. The other post has some excellent arguments against using bitwise operators as relates to maintainability and clarity. – J Trana Apr 07 '11 at 06:47
  • This is dangerous since somebody will come along and correct the code `|` => `||` thinking I made a mistake. – edA-qa mort-ora-y Apr 07 '11 at 06:47
  • Why was this downvoted? I commented on the maintainability and it definitely answered the question. Using non-shortcut operators in C++ is going to be dangerous no matter how you do it since it strays from normal behavior. – J Trana Apr 08 '11 at 05:58
  • Don't know, I didn't down-vote. – edA-qa mort-ora-y Apr 08 '11 at 06:14
  • @Pierre.Vriens I've definitely run into that situation a few times. This particular question is from long ago so it doesn't seem like perhaps the one to challenge. However, what does the process look like? – J Trana Sep 10 '18 at 01:40