Inline recursive functions?

D

Digital Puer

I got this on an interview: Is it possible to write inline recursive
functions?

I said yes, but there is no guarantee that the compiler will
definitely
inline your code even if you write "inline". Is this a right answer?
It seems to be independent of whether or not the function is
recursive.

Another question: how can you tell if the compiler has inlined your
code when you have used "inline"? Compare the size of the binary?
 
M

Mark P

Digital said:
I got this on an interview: Is it possible to write inline recursive
functions?

I said yes, but there is no guarantee that the compiler will
definitely
inline your code even if you write "inline". Is this a right answer?
It seems to be independent of whether or not the function is
recursive.

I'd say that's correct insofar as the C++ language is concerned. It's
syntactically permitted to declare the function inline, and the compiler
is likewise free to ignore this suggestion.

In practice, I suspect it's unlikely that a compiler would inline a
recursive function call, but it's not impossible. One could even
imagine "hybrid" situations where, for example, the compiler inlines the
initial function call but not any of the subsequent recursive calls.
Another question: how can you tell if the compiler has inlined your
code when you have used "inline"? Compare the size of the binary?

It's all platform dependent here. You could look at the binary, you
could look at the object file, you might find this information from a
debugger, and so on.
 
J

Juha Nieminen

Mark said:
It's
syntactically permitted to declare the function inline, and the compiler
is likewise free to ignore this suggestion.

Actually the compiler cannot ignore the 'inline' definition.
'inline' causes a different type of linking for the function than
it would be without it, especially if the compiler does not inline
the function.
Unlike eg. the 'register' keyword, which is nowadays a no-op,
'inline' isn't a no-op.
 
P

peter koch

I got this on an interview: Is it possible to write inline recursive
functions?

I said yes, but there is no guarantee that the compiler will
definitely
inline your code even if you write "inline". Is this a right answer?

Yes. There might be specific compilers where you can force the
compiler to do the inlining, but your answer is the correct generic
one.
It seems to be independent of whether or not the function is
recursive.

Another question: how can you tell if the compiler has inlined your
code when you have used "inline"? Compare the size of the binary?

I would examine the resulting code (e.g. by using the debugger and
looking at the relevant assembler code).

/Peter
 
P

peter koch

Actually the compiler cannot ignore the 'inline' definition.
'inline' causes a different type of linking for the function than
it would be without it, especially if the compiler does not inline
the function.
Unlike eg. the 'register' keyword, which is nowadays a no-op,
'inline' isn't a no-op.

This is not quite correct. The compiler is not required to inline. The
only requirement is that the compiler does not complain by seeing
multiple (equal) definitions of the inline function.

/Peter
 
L

Lionel B

I'd say that's correct insofar as the C++ language is concerned. It's
syntactically permitted to declare the function inline, and the compiler
is likewise free to ignore this suggestion.

In practice, I suspect it's unlikely that a compiler would inline a
recursive function call, but it's not impossible.

FWIW, not that unlikely: gcc, for example, will inline recursive
functions (or not, or to some depth) according to what appear to be
rather complex heuristics; it also allows some control of inline
recursion via parameters such as max-inline-recursive-depth.

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

inline functions 2
Proposed Standard Change: inline this 8
inline functions 2
Inline functions 33
inline vs. function pointers 36
gcc inline memcpy 7
inline functions 50
linking instanciated inline functions 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top