mortennobel / cpp-cheatsheet

Modern C++ Cheatsheet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

inline does not optimize for speed

yig opened this issue · comments

inline does not optimize for speed: https://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method/1759575#1759575.

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'.

Instead, its primary purpose is the following:

this function will be defined in multiple translation units, don't worry about it. The linker needs to make sure all translation units use a single instance of the variable/function.

It's also very useful for global variables (as of C++17).