In a text book I read inline functions cant contain recursion, go to, loops etc. Why? I dont think it will produce a syntax error if I do so.
-
2Which book is that? `inline` is only related to the One Definition Rule, it has no effect on what features you can use within a function. – BoBTFish Jun 04 '13 at 15:40
-
3possible duplicate of [Can a recursive function be inline?](http://stackoverflow.com/questions/190232/can-a-recursive-function-be-inline) – MK. Jun 04 '13 at 15:41
-
Aren't you confusing it with constexpr? those currently indeed are limited in many ways until C++14 hits the shelf. – Balog Pal Jun 04 '13 at 15:45
5 Answers
Your text book is wrong.
Firstly, it is important to understand the difference between the concepts of "declaring functions as inline
" and the actual "inlining of function calls". The former applies to the function itself, while the latter applies to each call independently.
Now, inline functions can contain absolutely anything. And almost any direct call to any function can be inlined. There are no restrictions whatsoever.
There's obviously no problems with inlining code that contains loops or local gotos. So, when your book says that inline functions "can't contain loops or gotos", it must be referring to quirks of some specific (and probably very old) compiler, which could not inline such functions for some implementation-specific reason.
Recursion is a different story. Obviously, it is not possible to completely inline all nested recursive calls if the depth of recursion is not known at compile time. Yet it is still possible to "unwrap" recursion to a specific limited depth. I.e. the compiler can inline the recursive calls, say, 5 levels deep and then proceed with a genuine (non-inlined) recursive call. This is not much different from the optimization technique known as "cycle unrolling". So, even recursive functions can be inlined. Some compilers can even let you control the depth of inline expansion for recursive functions.

- 312,472
- 42
- 525
- 765
This code is perfectly valid :
class Test
{
public:
inline void sayHello(int i);
};
void Test::sayHello(int i)
{
std::cout << "Hello "<< i << std::endl;
if (i>0)
sayHello(i-1);
}
int main(int argc, char** argv) {
Test t;
t.sayHello(10);
}
.. and recurses as expected.
(GCC 4.6.3)
In this case, the function is not inlined because inlining is just a hint for the compiler. Not a requirement.
As suggester in the above comments, you will find a more complete answer here: Can a recursive function be inline?

- 1
- 1

- 3,289
- 18
- 23
This sounds like confusion between the C++ concept of inline functions, and the optimisation of expanding a function call inline.
An inline function is one that is defined in every translation unit that uses it; unlike non-inline functions, which must be defined in just one translation unit per the One Definition Rule. There are no special restrictions on what these functions can do; in particular, they can call themselves recursively as you say.
The connection between inline functions and the optimisation is that many compilers need the definition to be available when compiling the call site; which generally requires the function to be inline to have a definition in more than one translation unit.
The book might mean to say that the recursive call can't be expanded inline, since that would mean that the generated code would have to contain a copy of itself; that is impossible. But there's still no reason why it shouldn't contain loops, branches or any other normal flow control statements.

- 249,747
- 28
- 448
- 644
As Mike said, the language definition has no such restrictions on inline
functions. What you sometimes see is a compiler warning that a function wasn't expanded inline because it used some language construct that the compiler won't inline, such as recursion, loops, etc. That's only telling you that the function wasn't expanded inline, so you didn't get an optimization that you might have wanted. The code is valid and the resulting executable will work correctly.

- 74,985
- 8
- 76
- 165
We can use loops,recursion etc in inline functions.But for a good programming practice we do not do so; because for each and every execution of instructions written in loop must me managed by your CPU.It can consume some extra space in your memory.