I know how inline functions work, but I've read they are only a suggestion to the compiler, and the compiler may decide to make an inline function non-inline and make a non-inline function inline. Since compilers know what they are doing far better than I do, why should I even bother making a function inline if the compiler is going to do what it thinks is best? How much do compilers listen to my "suggestions"?
Asked
Active
Viewed 548 times
1
-
1The inline keyword increases the chances of a function getting inlined. The reason why `inline` is a suggestion, is that it can often increase the executable size, if the function is too long. – lost_in_the_source Jun 29 '16 at 13:58
-
3If you really know what you're doing then you may _force_ compiler to use your _suggestion_. However you may also need to use inline just to put function body in your header file. Note that newest C and C++ differs about this (in particular in which compilation unit the _body_ of inlined function is placed) – Adriano Repetti Jun 29 '16 at 13:58
-
1It is essentially pointless these days to use this meaning for the `inline` keyword. Now, it is used almost exclusively as a means to bypass the one-definition rule (ODR). Let the compiler worry about when to (or not to) inline your code. – Cody Gray - on strike Jun 29 '16 at 13:59
-
1Also see: http://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method – NathanOliver Jun 29 '16 at 14:00
-
If you don't provide the code for the inline function to the compiler, it can't inline it — it has to make a function call. In C, if you have a `static` function then the compiler may (and quite often will) inline it even without the `inline` keyword. If the function is not `static`, it must be created as a referenceable function because code in another file might need to call it. So, providing the inline function allows the compiler to optimize when otherwise it might not. – Jonathan Leffler Jun 29 '16 at 14:01
-
@NathanOliver linked to by far the best answer for this question. – mkal Jun 29 '16 at 14:20
-
C and C++ are different languages. They differ in this subject. Pick one! – too honest for this site Jun 29 '16 at 14:55