TheLoneJoker
TheLoneJoker

Reputation: 1629

Alternatives to C "inline" keyword

From my course instructor, he has repeatedly emphasized and asked us not to use the "inline" keyword for functions. He says it is not "portable" across compilers and is not "standard". Considering this, are there any "standard" alternatives that allow for "inline expansion"?

Upvotes: 4

Views: 2042

Answers (4)

paxdiablo
paxdiablo

Reputation: 881423

Your course instructor is wrong. It is standard. It's actually in the current standard, right there in section 6.7.4 Function specifiers (C99). The fact that it's a suggestion to the compiler that may be totally ignored does not make it any less standard.

I don't think it was in C89/90 which may be what some embedded compilers use but I would give serious consideration to upgrading in that case.

However, even where inline is available, I generally leave those decisions up to the compiler itself since most modern ones are more than capable of figuring out how best to optimise code (and usually far better than I). The inline keyword, like register and auto, is not something I normally worry about at all.

You can use macros instead since that's relatively simple text substitution that generally happens before the compile phase but you should be aware of the limitations and foibles.

Or you can manually inline code (ie, duplicate it) although I wouldn't suggest this as an option since it may quickly become a maintenance nightmare.

Myself, I would write the code using normal functions without any of those tricks and then introduce them where necessary (and only if you can demonstrate that they're needed, such as a specific performance issue).

You should always assume that the coder who has to maintain your code is a psychopathic killer who knows where you live :-)

Upvotes: 19

Jens Gustedt
Jens Gustedt

Reputation: 78903

As others have said, inline was integrated to the C standard 11 years ago.

Other than was indicated, inline makes a difference since it changes the visibility properties of the function. In particular for large libraries with a lot of functions declared only static you might have one version of any these function in all object files (e.g when you compile with debugging switched on).

Please have a look into that post: Myth and reality about inline in C99

Upvotes: 2

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215259

Here, now it's "portable across compilers":

#if (__STDC_VERSION__ < 199901L)
#define inline
#endif
static inline int foobar(int x) /* ... */

By the way, as others have said, the inline keyword is just a hint and rather useless, but the important keyword is static. Unless your function is declared static, it will have external linkage, and the compiler is unlikely to consider it a candidate for inlining when it makes its own decisions about which functions to inline.

Also note that unlike in C++, the C language does not allow inline without static.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798636

As evil as they may be, macros are still king (although specific compilers may support extra capabilities).

Upvotes: 0

Related Questions