0

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

I have code similar to this:

return ( check1() && check2() && check3() && check4() );

The different check functions do other things besides just returning a value (eg printouts). It seems that the checkX functions are run sequentially and as soon as one returns a 0 that the remaining are not run. This makes sense to do it this way. What is the best way (in the C++ code) to make sure that each function runs regardless of the other return values?

Community
  • 1
  • 1
Stuart
  • 1,733
  • 4
  • 21
  • 34

5 Answers5

6
bool ret1 = check1();
bool ret2 = check2();
bool ret2 = check3();
bool ret4 = check4();

return (ret1 && ret2 && ret3 && ret4);
DougEC
  • 357
  • 1
  • 6
  • +1 and deleted my duplicate. (Yours was first) – Billy ONeal Oct 24 '12 at 20:37
  • The best answer here and the only one that should appear in real code. – Ed S. Oct 24 '12 at 20:56
  • `auto&` might be a better choice than `bool`, in case `chceckX()` return types that implement `operator&&`. – Robᵩ Oct 24 '12 at 21:12
  • @Robᵩ: if `checkX()` do return types that implement `operator&&`, then the original `&&` code doesn't short-circuit (or sequence). But they might be a mix, or the caller might not want to have to care what the return types are. But if we're speculating about funny return types then I'll say what I was saying in response to Pubby's answer, what if there are user-defined conversions required before `&&`? Then this code with `auto&` doesn't necessarily execute those in the same order as before. – Steve Jessop Oct 24 '12 at 21:28
2

use the bit-wise and operator

return ( check1() & check2() & check3() & check4() );

this will make each call before returning.


Node: This is a hack and is not recommend.

andre
  • 7,018
  • 4
  • 43
  • 75
  • Provided you don't mind what order the checks are executed. – Steve Jessop Oct 24 '12 at 20:44
  • But I'd say you should definitely add a comment about the reason you're using this, otherwise the next person to touch the code will think you're a fool for using arithmetic operators for logic, change it and introduce a bug. – Ivan Vergiliev Oct 24 '12 at 20:45
  • @IvanVergiliev Agreed this would be a maintenance nightmare. Its better to keep consistency. – andre Oct 24 '12 at 20:48
  • I'm downvoting this because it is a hack. If you need to call each function because they have side-effects then you should explicitly do so instead of changing logical operations to bitwise operations. – Ed S. Oct 24 '12 at 20:55
  • If the functions return int, you might get wrong results here (like when you have 1 & 2 & 4 & 8). (EDIT: removed my wrong comment about boolean & - that's Java) – Axel Oct 24 '12 at 20:58
1

one solution might be to capture results of each function in a bool variable and the perfoeming chexk on all 4 variables.

bool x1 = check1();
bool x2 = check2();
bool x3 = check3();
bool x4 = check4();
return (x1 && x2 && x3 && x3);

Bhanu Kaushik
  • 876
  • 6
  • 25
1
bool res = check1();
res &= check2();
res &= check3();
res &= check4();
return res;
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
0

If you don't care about the sequence in which the functions run, you could use:

return (check1() + check2() + check3() + check4()) == 4;

Another possibility would be to use pointers to functions, so you can invoke them in a loop:

typedef bool (*f)();

f checks[] = {check1, check2, check3, check4};

bool ret = true;
for (int i=0; i<4; i++)
     ret &= checks[i]();

return ret;

This doesn't help much for dealing with 4 functions, but if 4 is just a demo and there are really more (or there's a good possibility of that expanding), this makes it relatively easy to deal with a large number of functions when/if necessary.

If you cared more about conciseness than clarity, you could do something like this:

bool ret = check1();

return (ret &= check2()), (ret &= check3()), (ret & check4());
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Not sure I like the assumption that the `checks` functions have a common return type, the questioner's short-circuiting code only needs their return types to be convertible to `bool`. I am pretty sure the common return type can't be `void` ;-) – Steve Jessop Oct 24 '12 at 21:01
  • Oops -- quite right. Semi-fixed -- but yes, if it's depending on implicit conversions from different result types, these won't work. – Jerry Coffin Oct 24 '12 at 21:03
  • I don't understand why anyone would suggest anything other than breaking it out into four lines and returning the logical AND of the results. – Ed S. Oct 24 '12 at 22:01