Hello and welcome to C++ weekly.
I'm your host Jason Turner.
I'm available for code reviews, contracting,
and on-site training.
Now, in this episode I would like
to cover overloading of logical operators.
This is our logical and and logical or operators.
So I have this code.
I have already typed it out here.
I'm not going to make you watch me type it today.
And for perhaps a bit of a twist,
I have actually shrunk up all of the disassembly output
because that is not relevant to our code today.
I also am running a local version
of Compiler Explorer that allows me to execute the program,
which, at the moment, the website version does not allow.
But I have this very simple struct.
It is called s.
And when it is constructed it prints out a statement.
And then, I've got a value in here.
So I've overloaded this operator or.
And you know what--
I'm going to shift this up here.
So if we have this or operation, it's just
checking to see if the lefthand side is true
or the righthand side is true.
So if we were to create a main and create each of these
and do this, make s or make s.
And the first one is going to be true
and the second one is going to be false.
Now, I know that I have covered this in the past on this video,
that we like and use Boolean short circuiting when
it makes sense to.
So we would expect, in the normal case,
that this object being true would
cause this second branch to never execute
because it would say--
yes, this is true, so there's no reason to call this function.
And we can see that with a little bit of extra code.
So all we're doing is returning the value that was passed down.
But we can trace the function calls that were made.
So get bool of true or get bool of false.
Now, in this case, we can see that get bool
has been called exactly once because it called the first one
and said-- that is true, I am not
going to call the second one.
Now, if we do false here, it's going
to do the first one and the second one because it has to.
And if we put this and in here with a little bit of grouping,
it's going to execute the first one.
And then, it's going to keep trying until it sees
if it can complete this or not.
So if I make this true, then we'll
get two calls to get bool.
If I make this false--
because this is an and operation--
then I only get the one call.
The first one is false.
And then, the rest of it short circuits.
So this is kind of basic short circuiting logic with C++
or any of the ALGOL family of languages.
Now, let's go back to our s thing.
This thing with the overloaded operator or, it's
utterly impossible for this kind of logic
to ever work with that.
It has always got to execute each of the things.
It can't do short circuiting.
So let's go ahead and add in an ampersand, logical ampersand
and operation here like this, and put in this.
And let's do the exact same thing and see what happens.
So I have now done this make s false.
And I'm expecting, or would like to expect,
that some sort of short circuiting would happen here
and this code would never execute, just
like in the case with the regular Boolean value.
But that's impossible.
So I get make s called three times.
I get four of these values generated.
Like, it's just, none of it does exactly what
we would expect it to do.
So this is why if you're ever reading any book that says,
never overload logical and or logical or the comma operator--
yes, the comma operator can be overloaded as well--
this is why.
It's because with this overloading
it is impossible to get the semantics
of the built-in operator.
So be aware of that and don't overload
your logical operators.
Thanks for watching C++ weekly.
Be sure to subscribe.