Jump to content


A little C++ curiosity...


5 replies to this topic

#1 poita

    Senior Member

  • Members
  • PipPipPipPip
  • 322 posts

Posted 22 September 2009 - 04:19 PM

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.

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5340 posts
  • LocationSanta Clara, CA

Posted 22 September 2009 - 04:21 PM

No; when calling a function all arguments are always completely evaluated.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 poita

    Senior Member

  • Members
  • PipPipPipPip
  • 322 posts

Posted 22 September 2009 - 04:55 PM

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?

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 5340 posts
  • LocationSanta Clara, CA

Posted 22 September 2009 - 05:00 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&&.
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 poita

    Senior Member

  • Members
  • PipPipPipPip
  • 322 posts

Posted 22 September 2009 - 05:40 PM

Yay for consistency.

#6 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 22 September 2009 - 08:05 PM

Reedbeta said:

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
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users