views:

123

answers:

3

Is it worth it to use the inline keyword or the compiler is smart enough to know when he needs to inline a function ?

+3  A: 

The compiler is pretty smart, and has multiple metrics to figure out if something is worth inlining. But sometimes however the developer will have knowledge about how the application is going to be run and will know to inline something that the compiler doesn't do automatically. However, I would never inline stuff manually unless I had a done some benchmarks and found that inline would improve my performance.

You can read more about how GCC uses inline.

bramp
+3  A: 

Its always worth being explicit about intention.

Its also worth noting that the compiler doesn't even need to inline if it thinks its better not too.

Goz
+5  A: 

Yes, it is smart enough. But what has made no progress at all in the past 40 years is the way C programs are built. It is still one source code file at a time.

So to get a function inlined in more then one .c file you put the function definition in the .h file. And if you don't mark them inline, the linker will complain about the multiple definitions.

Hans Passant
+1, `inline` nowadays means "allow multiple definitions". The fact that it's also a hint to the compiler to "make calling this function fast" is an unrelated and irrelevant side-effect :-) I think it's not that "the way programs are built" hasn't advanced - some compilers/linkers have. But the lowest common denominator, that the standard allows, hasn't advanced.
Steve Jessop
Saying `inline` means "allow multiple definitions" is simply wrong. This question is about the C language, not C++. C does not have `extern inline` functions, the `inline` keyword is invalid without `static`, and multiple definitions of a function are never allowed.
R..