Is there a logical & operator in C++? e.g. an operator that works just as && except that it also evaluates later arguments even if some preceding ones have already evaluated to false? The operator & is the bitwise and operator I understand.
-
6Why would you need that? For all use cases I can imagine, you should just fix that mess that mixes logical conditions with mandatory side effects. – Aug 01 '11 at 13:46
-
@Ziyao Wei - read again:) at delnan - I agree with you. – Kiril Kirov Aug 01 '11 at 13:47
-
Post some use cases as to how exactly you want to it, and for what purposes? – Nawaz Aug 01 '11 at 13:49
4 Answers
The operator & is indeed the bitwise operator. I'm assuming you have something like
if ( f() && g() ) { /*do something*/ }
and you want both f() and g() to execute, regardless of whether one of them was evaluated to false. I suggest you do something else instead:
bool bF = f();
bool bG = g();
if ( bF && bG ) { /*do something*/ }
This also provides better readability and doesn't confuse other programmers who try to maintain your code. In the long run, it's worth it.

- 253,575
- 64
- 457
- 625
-
1
-
One can change `if ( f() && g() )` into `if ( (int)f() + (int)g() == 2 )` which will force both operand to evaluate. – Nawaz Aug 01 '11 at 13:54
-
3What if f() and g() return int? if f() returns -1 and g() returns 1, f() && g() would yield true, while your condition would yield false... – Luchian Grigore Aug 01 '11 at 13:55
-
I'm not recommending this, but a way to make @Nawaz' solution work would be to first cast it to a bool, then back to an int. – Benjamin Lindley Aug 01 '11 at 14:08
-
1@Benjamin: That would also make it so easy to read. Especially the intent aspect of the statement. – Martin York Aug 01 '11 at 15:35
There is no such "always execute" operator in C++.
My first inclination is that instead of looking for a new operator, you should re-evaluate what your methods do to eliminate any side effects that mandate they be executed. It may be possible in this way to simply be able to use &&
and be happy.
However if you actually want to do all the operations in sequence and then see if they all succeeded, probably Luchian Grigore's answer would be the best. It clearly delineates that it's sequential steps that always need to execute. There is one more option which may or may not be less clear:
// Each method needs to execute in sequence but we use "success" to track overall success. The order of operands to `operator&&` shouldn't be changed.
bool success = f1();
success = f2() && success;
success = f3() && success;
if(success) ...

- 95,107
- 10
- 109
- 188
-
1+1 Useful technique to learn. Though I always start with `bool success = true;` That way all the conditional evaluations look the same. – Martin York Aug 01 '11 at 15:36
There is no logical &
, only a bitwise &
.
if you want to avoid short-circuiting of logical statements, you need to use a proxy the won't be optimized away by the compiler to achieve it (such as a variadic meta-template).

- 25,836
- 3
- 63
- 101
If you overload the operator &&, it won't short circuit.
struct Bool {
bool val;
Bool(bool f): val(f) {}
operator bool() {
return val;
}
};
bool operator&&(Bool a, Bool b) {
return (bool)a && (bool)b;
}
ref: point 19, section 13.9 in the C++ FAQ lite
Though as mentioned there, this is a very bad idea and confuses people. You might want to do it in a very limited way though if you have a very special case.

- 41,293
- 8
- 87
- 103
-
2
-
-
1Wow. This scares me from all the funky side effects it might introduce from all the code that assumes (correctly) that short-circuit is required. (e.g. if ((a != null) && (a.foo = 123)) ...) – Joe Aug 01 '11 at 13:50
-
-
You can't do that, you need at least a user-defined parameter. And if you could, I think it would go in an infinite loop... – Luchian Grigore Aug 01 '11 at 13:53
-
I know it's **bad** - but I think it's good to know these things in any case. @Luchian Grigore - gah. edited. – sje397 Aug 01 '11 at 14:05
-
Oh I didn't see Bool was a defined type... in that case, yeah, that works :) – Luchian Grigore Aug 01 '11 at 14:08
-