3

I'm a newbie programmer. There are these line in our text book, sumitha arora class 12 ix edition:

The inlining does not work for the following situations:

  1. For functions that return values and are having a loop or switch or goto.

  2. For functions not returning values, if a return statement exists.

If 1 and 2 are true then how are inline functions possible to create?

P.S. the lines are exactly the same from the book no alterations

3
  • Note that inline, is just a request for inlining. The compiler is not obliged to honour that request. Don't assume that if you specify inline and it meets the criteria you have posted, that the code will be inlined. Commented May 15, 2014 at 6:09
  • You mean we can never say if a function is an inline function or not? Commented May 15, 2014 at 6:15
  • The compiler is free to inline it or not. Some compilers have special extensions to actually force a function to be inlined, but apart from that, there is no way to enforce this. The question is why you would want to do this. Usually, the compiler vendors know much better when to inline something. Commented May 15, 2014 at 6:46

3 Answers 3

2

I don't know what compiler is referred in the book. In general it will depend on the actual compiler, what can be inlined or when a function body will be generated.

The statements doesn't exclude each other as you might assume. The general condition to inhihbit inline is a branch in the function except a simple if statement. The branches in (1) are the loops and the goto. The branch in (2) is the jump to the end of the function on return;

Edit:

It is highly dependent on the compiler. Nested if might work. But switch statements differs, as they are often implemented with a jump table instead of several if statements. The jump table includes location similar to the goto labels. That might be more difficult for the actual compiler. Therefore it might be sensible to make a distinction.

Sign up to request clarification or add additional context in comments.

1 Comment

If if statements can work could nested if statements work or is it too complicated? Why can't switch statements work?
1

Inline functions are basically written when the code is small and the stack push/pop takes up much of the overhead. What they do is, they expand the function where they are written. Since there is no push/pop associated, you cannot expect value to be returned. Although, C++ can convert simple functions with only a return statement to inline functions.

Here's a C++ code:

inline int max(int a, int b) { return (a > b) ? a : b; } 

Then, a statement such as the following:

a = max(x, y); 

may be transformed into a more direct computation:

a = (x > y) ? x : y; 

4 Comments

Can we use inline functions in classes only?
You can use inline functions as class member functions and non-member functions as well. See this and this.
If they say linkage is external by default does that mean any change is not reflected back?
The 'direct computation' a = (x > y) ? x : y; looks like a macro. I would emphasize that this is not a macro, where the items x or y could be evaluated more than ones.
0

IMHO, the C++ standard does not define restrictions for inlining. Inline functions are defined in section 7.1.2.2:

A function declaration with an inline specifier declares an inline function . The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call;

Compilers usually have some heuristics to decide if a function will be inlined or not. This balances the cost to perform an actual function call (push arguments on the stack, jump etc) with the effects of having a larger function (the most obvious drawback is an increased size of the resulting binary), but the concrete rules are left open for the compiler implementer.

If the book is not talking about a specific compiler, I see not reason why this statement is true in general. It may a rule of thumb, though.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.