Sobes.tech
Junior — Middle

Is a function with the inline keyword always inserted into the call code during compilation?

sobes.tech AI

Answer from AI

No, the function with the keyword inline is not necessarily always inserted into the call code during compilation. The inline keyword is merely a suggestion to the compiler that it is preferable to replace the function call with its body to reduce call overhead.

However, the compiler may ignore this suggestion if:

  • the function is too large
  • there are recursive calls
  • optimizations are disabled

Also, if a function is declared inline but its definition is not available at the time of the call's compilation, inlining is impossible.

Example:

inline int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(2, 3); // the compiler may replace the call with "2 + 3"
}

In the end, inline is a hint, not a guarantee of inlining.