PDA

View Full Version : A little C++ curiosity...


poita
09-22-2009, 09:19 AM
This has no practical consequences for me, it's just something that I'm curious about.

If I were to create a function

bool foo(bool a, bool b) { return a && b; }

And use it...

foo(false, function_with_side_effects());

Would the compiler be allowed to inline it such that the function with side effects isn't called? In other words, could it inline it to this:

false && function_with_side_effects();

Again, this isn't causing me any problems; it's just a curiosity that recently crossed my mind.

Reedbeta
09-22-2009, 09:21 AM
No; when calling a function all arguments are always completely evaluated.

poita
09-22-2009, 09:55 AM
Well, yes. I suppose the implication is that this rule applies to functions that are inlined as well?

What about overloads of operator&&? Are they all special functions, or are they only special for primitive types?

Reedbeta
09-22-2009, 10:00 AM
Yes; inlining a function doesn't change the fact that all arguments are evaluated. As for overloads of operator&&, I'm 95% sure they behave just like regular functions, and the short-circuiting behavior only applies to the built-in operator&&.

poita
09-22-2009, 10:40 AM
Yay for consistency.

.oisyn
09-22-2009, 01:05 PM
Yes; inlining a function doesn't change the fact that all arguments are evaluated. As for overloads of operator&&, I'm 95% sure they behave just like regular functions, and the short-circuiting behavior only applies to the built-in operator&&.
Correctamundo