Search code examples
cinline-functions

When is the "inline" keyword effective in C?


Well, there is no guarantee by the standard that inline functions are actually inlined; one must use macros to have 100 % guarantee. The compiler always decides which function is or is not inlined based on its own rules irrespective of the inline keyword.

Then when will the inline keyword actually have some effect to what the compiler does when using modern compilers such as the recent version of GCC?


Solution

  • It has a semantic effect. To simplify, a function marked inline may be defined multiple times in one program — though all definitions must be equivalent to each other — so presence of inline is required for correctness when including the function definition in headers (which is, in turn, makes the definition visible so the compiler can inline it without LTO).

    Other than that, for inlining-the-optimization, "never" is a perfectly safe approximation. It probably has some effect in some compilers, but nothing worth losing sleep over, especially not without actual hard data. For example, in the following code, using Clang 3.0 or GCC 4.7, main contains the same code whether work is marked inline or not. The only difference is whether work remains as stand-alone function for other translation units to link to, or is removed.

    void work(double *a, double *b) {
      if (*b > *a) *a = *b;
    }
    
    void maxArray(double* x, double* y) {
        for (int i = 0; i < 65536; i++) {
            //if (y[i] > x[i]) x[i] = y[i];
            work(x+i, y+i);
        }
    }