I'm new to c++, can anyone explain during what stage of the compilation do inline functions get expanded in detail?
-
They need to be expanded at linkage stage. – πάντα ῥεῖ Apr 23 '19 at 17:08
-
1What exactly do you mean by "stage"? – melpomene Apr 23 '19 at 17:08
-
@πάνταῥεῖ surely its implementation dependant? I'd have thought most compilers would insert the inlined code at compile time? – Alan Birtles Apr 23 '19 at 17:12
-
1@AlanBirtles Since when does _linkage stage_ not count for _"compile time"_? – πάντα ῥεῖ Apr 23 '19 at 17:14
-
Its impossible to tell. All you can know is if the resulting binary is inlined. Most modern compilers ignore the "inline" keywords in terms of actually inlining the function (the compiler is much better at making the decision). It is only required to make sure you don't have double definitions at the linker stage. – Martin York Apr 23 '19 at 17:14
-
"stage" here reflects the phase of a compiler. (eg: Target code generation). – avi_vavin Apr 23 '19 at 17:15
-
6There is no "standard compiler architecture". Heck, C++ isn't even required to be compiled as far as I know. – melpomene Apr 23 '19 at 17:18
-
@user_vavin A compiler might choose to precompile some kind of _assembly template_ for inlined stuff, and insert that later on during linkage stage replacing the correct addresses. – πάντα ῥεῖ Apr 23 '19 at 17:21
-
1You seem not to understand what the compiler and linker is doing behind the scenes, this question doesn't exactly make sense. – Passer By Apr 23 '19 at 17:36
-
I suggest you practice looking things up. It takes only seconds to find for your review such info as in : https://stackoverflow.com/q/6264249/2785528 or https://en.cppreference.com/w/cpp/language/translation_phases or even http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compile.html. Lots of opinions and software is remarkably flexible. – 2785528 Apr 23 '19 at 17:48
2 Answers
When you say "inline" you could be talking about two different things.
In c++ there is the inline
keyword which is used by the linker. A good explanation can be found here.
You could also be talking about the compiler optimization, function inlining. Function inlining is done by the optimizer so it is hard to say exactly when it happens. The decision to inline a function could be influenced by the target architecture and it's cache size along with many other things. In general I guess you could say that function inlining is done during optimization.

- 1,027
- 12
- 28
Ideally at optimisation phase, since there is a sweet synergy with other optimising paths.
For instance, constant propagation. You may infer that your function is called with a particular value rather than an arbitrary argument. Propagating this value inside the function might simplify its body, becoming tight enough to qualify for inlining.
This combined approach is key to get zero-cost abstractions.

- 3,778
- 1
- 20
- 19