c++inlineone-definition-ruleinline-functions

When should I write the keyword 'inline' for a function/method?


When should I write the keyword inline for a function/method in C++?

After seeing some answers, some related questions:


Solution

  • Oh man, one of my pet peeves.

    inline is more like static or extern than a directive telling the compiler to inline your functions. extern, static, inline are linkage directives, used almost exclusively by the linker, not the compiler.

    It is said that inline hints to the compiler that you think the function should be inlined. That may have been true in 1998, but a decade later the compiler needs no such hints. Not to mention humans are usually wrong when it comes to optimizing code, so most compilers flat out ignore the 'hint'.

    Note: Generally, declaring templates inline is pointless, as they have the linkage semantics of inline already. However, explicit specialization and instantiation of templates require inline to be used.


    Specific answers to your questions: