1

I've searched for the definition of an inline function, and basically all sources provide this answer: "An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory."

I thought an inline function would be useful to work with functions and pass the value of an argument x, which is in main() scope, into function. That way, I thought the argument in main() scope would be affected and changed since the inline function does not copy the value but writes the actual code into the main() scope.

I expect the argument to be changed, yet it did not. Then what is the problem? Or is there something I've msised?

  • 6
    Inlining should never change the semantics of your program. It is only an optimization that keeps the behavior as if the code was not inlined. – wohlstad Dec 22 '22 at 07:06
  • 1
    It doesn't literally copy and paste the code, it copies the instructions but any variables in each function remain separate. A [mre] would help explain what you're confused about – Alan Birtles Dec 22 '22 at 07:07
  • 1
    `inline` is simply a suggestion that the compiler actually inline the code. It is still useful to avoid multiple definitions. https://en.cppreference.com/w/cpp/language/inline – Retired Ninja Dec 22 '22 at 07:21
  • 1
    In the old days when we had dumber compilers, `inline` did, more or less, what you think it does. These days the compiler will decide whether or not to inline a function depending on which option suggested better performance and all it really does is prevent breaking the One Definition Rule. – user4581301 Dec 22 '22 at 07:21
  • 2
    That's a common definition of an inline function, but it is **not** the meaning of the `inline` keyword in C++. – john Dec 22 '22 at 07:25
  • Maybe what you are trying to do can be achieved with a lambda function capturing some part of the surrounding scope by reference, but it's unclear what your goal is. – Ulrich Eckhardt Dec 22 '22 at 07:39
  • 5
    The argument-passing is also inlined; `void f(int x) { x += 1; } int main() { int y = 0; f(y); }`more or less becomes `int main() { int y = 0; { int x = y; x += 1; }}`. If inlining changed the semantics of programs it would be worthless. – molbdnilo Dec 22 '22 at 07:50
  • Handy reading: [What exactly is the "as-if" rule?](https://stackoverflow.com/q/15718262/4581301). And more formally, https://en.cppreference.com/w/cpp/language/as_if – user4581301 Dec 22 '22 at 16:26

1 Answers1

0

To illustrate my answer, I've built a worked example showing How the compiler handles functions, inline functions, and macros.

Lines 19-21 show the "call" procedure for the inlined function:

__attribute__((always_inline)) inline void InlineIncrement(uint8_t y) {
    y += 1;
}

...

InlineIncrement(x);

Becomes

movzx   eax, BYTE PTR [rbp-1]
mov     BYTE PTR [rbp-3], al
add     BYTE PTR [rbp-3], 1

Rather than adding one to x directly (which is stored at rbp-1) it first copies x to rbp-3, then adds one to the value stored in rbp-3, leaving rbp-1 unchanged.

As @molbdnilo points out in his comment, this is equivalent to the following code:

uint8_t x = 0;
{
    uint8_t y = x;
    y += 1;
}

To actually effect a "copy and paste" you will instead need to use preprocessor macros, however these can quickly become unwieldy and may have counterintuitive results.

If your aim is to allow an input variable to be modified by the called function, then consider passing by reference instead.

Malacandrian
  • 223
  • 1
  • 8