c++gccclanginline-code

C++'s "inline" - how strong a hint is it for GCC and Clang/LLVM?


In C++, the keyword "inline" serves two purposes. First, it allows a definition to appear in multiple translation units. Second, it's a hint to the compiler that a function should be inlined in the compiled code.

My question: in code generated by GCC and Clang/LLVM, does the keyword "inline" have any bearing on whether a function is inlined? If yes, in what situations? Or is the hint completely ignored? Note this is a not a language question, it is a compiler-specific question.


Solution

  • [Caveat: not a C++/GCC guru] You'll want to read up on inline here.

    Also, this, for GCC/C99.

    The extent to which suggestions made by using the inline function specifier are effective (C99 6.7.4).

    • GCC will not inline any functions if the -fno-inline option is used or if -O0 is used. Otherwise, GCC may still be unable to inline a function for many reasons; the -Winline option may be used to determine if a function has not been inlined and why not.

    So it appears that unless your compiler settings (like -fno-inline or -O0) are used, the compiler takes the hint. I can't comment on Clang/LLVM (or GCC really).'

    I recommend using -Winline if this isn't a code-golf question and you need to know what's going on.