Bloomberg LP Interview Question
Software Engineer / Developersthe question is absolutely correct. merely specifying function as inline doesnt make it inline. you can only hint the compiler to make it inline. as others pointed, following are the scenarios in which even when specifying a func as inline wont make it inline.
1. when using resursion
2. when using loops inside functions
3. when taking function addresses
Inline function is a way to achieve inline expansion and the keyword inline is a programmer term to "advise" the compiler to consider for the inline expansion:
Advantages: The lifecycle of function call takes resources in calling, allocating and deallocating and generally the present day compiler are well equipped to support this in an optimized way. It is also considered that this advise of programmer in the language to inline expand a function is at the discretion of compiler optimization strategy.
Coming to have a opinion for this question that when will the compiler reject this advise,ps. not big code,
Reasoning: what makes an inline expansion to a non deterministic way.
1. expansion of a recursive function who return values depend on highly recurred run time parameter. Compiler will go crazy and will say bye bye to inline
what else,
2. Big loop. for the same reason.
3. Calling another function, compiler cannot find all the dependent functions nature as inline expansion. Of course its possible but huge overhead.
Thanks AK,
[WIKI]: " it is not always possible to inline a subroutine. Consider the case of a subroutine that calls itself recursively until it receives a particular piece of input data from a peripheral. The compiler cannot generally determine when this process will end, so it would never finish inlining if it was designed to inline every single subroutine invocation. Thus, compilers for languages which support recursion must have restrictions on what they will automatically choose to inline."
The compiler cannot inline a function if:
The function has a variable argument list;
is recursive;
is virtual (direct calls to virtual functions can be inlined);
uses inline assembly, (unless compiled with /Og, /Ox, /O1, or /O2);
if the program takes the address of the function and the call is made via the pointer to the function (direct calls to functions that have had their address taken can be inlined);
the function and the caller use different types of exception handling (C++ exception handling in one, structured exception handling in the other).
I must say that this is an out-of-date question. Nowadays compilers and linkers will inline and not inline whenever they feel like it, even the same function! Herb Sutter's 'Exceptional C++ Style', page 190 has a question: "3) .. What kinds of functions are guaranteed never to be inlined?" And his answer is 'none'. And this book is copyright 2005. (Sutter is both head (I think) of MS's compiler project, and of the C++ standard committee.) Now, whether the /compiler/ (and not the linker) will inline stuff, I can only think that it'd have to be when a function is 'extern' to a compilation unit. But, this is uninteresting, since the linker can still do it. So the only answer worth knowing is 'no'. Unless you happen to know your compiler's limits and quirks, but then that's not a generic C++ question.
Nope - Sutter's 'Exceptional C++ Style' says, pg 192: "It [inline] doesn't affect other parts of the standard language, in that writing 'inline' on a function does not change how you use the function (for example, you can still take the function's address) ..."
Sunny, please only place REAL interview question here. This question is not stated correctly. If you don't declare a function "inline", how can a C++ compiler treat a function(even it is small) as inline? Did you get an interview and remember the question correctly?
- kulang July 31, 2010