I know in advance that, when writing a program in C or C++, even if I declare a function as "inline" the compiler is free to ignore this and decide not to expand it at each (or any) call.
Is the opposite true as well? That is, can a compiler automatically inline a very short function that wasn't defined as inline if the compiler believes doing so will lead to a performance gain?
Two other subquestions: is this behaviour defined somewhere in the ANSI standards? Is C different from C++ in this regard, or do they behave the same?
c
standards since I last looked at that sometime in the 1990s. My comment above was just aboutc++
– Systematicsinline
keyword is maybe a hint for the compiler. It has nothing to do anymore with whether or not inlineing occurs. – Postmeridianstatic
(anonymous namespace?), the same probably applies to C++. – Microsomestatic
, then you can put it into a header file. The other exception is LTO (link time optimization). But for the default case (function in a .cpp file, without LTO), the compiler won't (because it cannot) inline the function, if it is in another compilation unit. – Keshiakesia