Oring multiple values

J

James Aguilar

Suppose that I want to get the or of several values. Which of these ways
would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

Assume that each fn type function does stuff that needs to be done all the
time -- in other words, even if it returns false, it is useful.

- James
 
V

Victor Bazarov

James said:
Suppose that I want to get the or of several values. Which of these ways
would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

Assume that each fn type function does stuff that needs to be done all the
time -- in other words, even if it returns false, it is useful.

Assuming each 'fnX' returns some type for which conversion to 'bool' is
defined or the operator| is defined (like an integral rvalue), there is no
difference between the two approaches. I'd probably use the latter simply
because it's all in one line and therefore easier to understand.

V
 
A

Alf P. Steinbach

* James Aguilar:
Suppose that I want to get the or of several values. Which of these ways
would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

Assume that each fn type function does stuff that needs to be done all the
time -- in other words, even if it returns false, it is useful.

In that case, perhaps

bool const test = !!(fn1() + fn2() + fn3() + fn4());

which can not be misread as shortcut evaluation, and less probably
"fixed" changing "|" to "||" by some maintainance programmer.

Hth.,

- Alf
 
J

Jay Nabonne

Suppose that I want to get the or of several values. Which of these ways
would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

Assume that each fn type function does stuff that needs to be done all the
time -- in other words, even if it returns false, it is useful.

- James

Beware of bitwise or'ing function values. I ran into a problem once where
code was running differently on DOS vs. the Macintosh. It turns out that
the evaluation order of the '|' operands was different for the two
compilers, so the functions were being called in different order.

FYI

- Jay
 
V

Victor Bazarov

Alf said:
* James Aguilar:



In that case, perhaps

bool const test = !!(fn1() + fn2() + fn3() + fn4());

which can not be misread as shortcut evaluation, and less probably
"fixed" changing "|" to "||" by some maintainance programmer.

With the only problem that if fn<odd> return -1 and fn<even> return 1,
it has a rather strange outcome, don't you think?

V
 
A

Alf P. Steinbach

* Victor Bazarov:
With the only problem that if fn<odd> return -1 and fn<even> return 1,
it has a rather strange outcome, don't you think?

We must assume that those are 'bool' functions, "even if it returns false".
 
R

Real Name

Victor Bazarov said:
Assuming each 'fnX' returns some type for which conversion to 'bool' is
defined or the operator| is defined (like an integral rvalue), there is no
difference between the two approaches. I'd probably use the latter simply
because it's all in one line and therefore easier to understand.

The two approaches are different: in your first approach every fnX() will be
executed, in the
last one the execution of fnX depends on the result of fn(X-1). We careful
with that.
 
K

Karl Heinz Buchegger

Real Name said:
The two approaches are different: in your first approach every fnX() will
be
executed, in the
last one the execution of fnX depends on the result of fn(X-1). We careful
with that.

You may have missed that in the first version the OP used 'logical or' while
in the second version he used 'bitwise or'. Shortcut evaluation is performed
only in the case of 'logical or'. So if all those functions return a bool
result, both versions are indeed identical.
 
J

Jerry Coffin

James said:
Suppose that I want to get the or of several values. Which of these ways
would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

I think I prefer the second, but as long as you're only dealing with
four functions, it probably doesn't make a lot of difference which you
use. If four functions is basically just an example, and it might
really be 20 functions instead, you might want to consider
alternatives. The first obvious one would be to create an array of
pointers to functions, and write a loop to invoke them in turn.

It would probably also be possible to create a template to handle this,
but I have a hard time imagining it being used enough to justify
putting much work into it.
 
R

red floyd

Karl said:
You may have missed that in the first version the OP used 'logical or' while
in the second version he used 'bitwise or'. Shortcut evaluation is performed
only in the case of 'logical or'. So if all those functions return a bool
result, both versions are indeed identical.

Is there a guarantee of evaulation order in the second case? If not,
and the fnX() have side effects, then there is a difference. Yes, the
order of the bitwise operator is defined, but as I understand it, the
compiler is free to evaluate fnX in any order it wants, so long as the
bitwise-or operators are done in the proper order.
 
V

Victor Bazarov

red said:
Is there a guarantee of evaulation order in the second case? If not,
and the fnX() have side effects, then there is a difference. Yes, the
order of the bitwise operator is defined, but as I understand it, the
compiler is free to evaluate fnX in any order it wants, so long as the
bitwise-or operators are done in the proper order.

Whether "ORing" is done "in the right order", doesn't really matter, there
can be no overflow. You're absolutely right, if there are side effects,
the result can be different.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top