kadina
kadina

Reputation: 5376

When inline function actually replaces the code and is it possible to put a break point?

I have some questions about inline functions as mentioned below.

1) I read that function call of an inline function will be replaced by the code at compile time. If this is true, can any one please let me know how it will work with virtual functions because virtual function is a concept based on late binding.

2) Is it possible to put a break point inside an inline function?

3) If an inline function is simply replacing the function call and if the inline function has a return statement, why the caller of the inline function is not terminated with the return statement of the inline function?

4) Is there any way to force the gcc compiler to use the function as inline instead of just a suggestion to the compiler?

Upvotes: 2

Views: 375

Answers (3)

Anatoly Makarevich
Anatoly Makarevich

Reputation: 147

1) Virtual functions will not get inlined, unless the compiler "sees" that the same code will be called in various cases (unlikely to happen).

2) Yes, it is possible, especially considering many IDE's use macro tricks (and other things) to not cut away code in "debug" modes.

3) If a function is inlined, it is actually the code being inserted in every call location instead of a pointer to the function being called.

4) Cannot answer this as I have little experience with GCC. "Release" mode is more likely to set inlining, though.

Upvotes: 1

D.R.
D.R.

Reputation: 21194

1) Simple answer: It does not work with virtual functions most of the time. See Are inline virtual functions really a non-sense? for a more detailed answer.

2) Yes you can. Compilers normally do not perform any inlining for debug builds.

3) Of course the compiler does not put the "ret" assembly instruction in the calling code. Think abut it that way:

// before inlining:
int MyFunc () { return 5; }
int i = MyFunc();

// after inlining:
int i = 5;

4) See here for an answer: How do I force gcc to inline a function?

BTW: Using Google would have answered all your questions in less time ;-)

Upvotes: 2

rlbond
rlbond

Reputation: 67749

  1. A virtual function will not be inlined unless the type of the object is known at compile time (e.g., the call is made with an object, not a reference or pointer).

  2. You can put a breakpoint in an inline function if you compile it in debug mode (which nearly every compiler supports). In debug builds, functions are not inlined.

  3. The inline function is not simply copied, that would be wrong and stupid. The call is intelligently replaced with the appropriate code.

Upvotes: 2

Related Questions