1

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)?

5
  • 3
    Inlined functions don't go on the stack. Commented Sep 6, 2018 at 10:28
  • I thought that the one of the points of inlining is to avoid a call to the function and instead put the body on the stack? Commented Sep 6, 2018 at 10:30
  • 2
    They are inline by default, but inline has almost nothing to do with function inlining. inline tells the linker that there can be several (identical) function definitions. Commented Sep 6, 2018 at 10:30
  • You generally can't execute code from the stack, for security reasons: it'll be in a segment marked not executable. But I'm not sure why you'd want to, or need to? Typically if a function is inlined then the compiler will just compile the calling code as if you'd copy-pasted the inline function definition to the call site. Commented Sep 6, 2018 at 10:33
  • Stacks hold data, not function bodies. The call stack of a thread in particular holds local variables and return addresses of all active functions. Commented Sep 6, 2018 at 12:05

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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".
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.
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.