I have read, multiple times, that all member functions defined inside a class are inlined by default. Does that mean that the compiler will always put the body code of the function on the stack if it is suitable( i.e. the code does not contain any loops or function calls)?
1 Answer
all member functions defined inside a class are inlined by default.
No, they are inline by default.
That means that the definition can and must be provided in every translation unit where the functions are used. For member functions that means, where an instance of the class is used.
inline also has a hinting effect about machine code inlining of calls. The compiler can follow or ignore that hint at its discretion, per call.
3 Comments
Jörg W Mittag
Today, it is probably more like: "
inline also had a hinting effect, back in the Dark Ages, when compilers were much more stupid than they are now".IInspectable
The inline specifier (as well as implicit inlining) has a distinct meaning in C++: It declares an inline function. This is not just a hint. It has immediate effect on the linker's ability to produce a binary.
Cheers and hth. - Alf
@IInspectable: When you feel the need to rephrase the answer in a comment, why not post that as a separate answer, or edit the existing answer? Editing would be OK by me. It's how SO was intended to work, back in the day.
inlineby default, butinlinehas almost nothing to do with function inlining.inlinetells the linker that there can be several (identical) function definitions.