2

I've just been researching the use and benefits/pitfalls of the C++ keyword inline on the Microsoft Website and I understand all of that.

My question is this: if the compiler evaluates functions to see if inlining them will result in the code being more efficient and the inline keyword is only a SUGGESTION to the compiler, why bother with the keyword at all?

EDIT: A lot of people are moaning about my use of __inline instead of inline. I'd like to point out that __inline is the Microsoft specific one: so it's not wrong, it's just not necessarily what you're used to. (Also fixed the website link)

EDIT2: Re-formatted the question to indicate the inline keyword (used across all of C++) instead of the Microsoft-specific __inline keyword.

Edward
  • 235
  • 4
  • 18
  • Shouldn't the title be "why bother with __inline keyword in Visual Studio"? – Cubbi Jun 26 '13 at 17:19
  • @Edward: They're moaning because you wrote `Why bother with the __inline keyword in C++?`; they wouldn't had you written `Why bother with the __inline keyword in VC++` i.e. a Microsoft compiler implementation of the C++ langauge standard not the C++ language standard itself. – legends2k Jun 26 '13 at 17:35

3 Answers3

8

Firstly, it is not __inline, but inline.

Secondly, the effect inline has within the One Definition Rule is undeniably significant. It allows you to define the functions multiple times and have the compiler to handle it.

Thirdly, with regard to the actual inilining this is your way to express your opinion about that function not only to the compiler but also to those who might read your code later. In many cases it is a way to let off the steam, so to say. Basically, it is a way for you to tell the others and yourself: "I feel that this function is too small (or too specialized) to justify the calling overhead, so don't hold me responsible for this travesty. I did all I could. If not for your stupid company-wide coding standard, I would've made it a macro". In that regard it is a sort of formalized comment.

Fourthly, seeing that you used an implementation-specific spelling of the keyword, I'd note that some implementations offer you alternative keywords that give you the opportunity to be more... er... persuasive in your desire to have that function inlined. In MS compiler that would be __forceinline.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • I'm aware of the `__forceinline` keyword (hence why I didn't mention it and was specifically talking about the `inline` keyword) and my personal preference is to put ALL functions definitions in the .cpp file instead of shoving some in the .h so it looks neater. But thanks for explaning the main points for me (I'm aware others have different preferences). – Edward Jun 26 '13 at 17:06
  • @Edward You cannot get all function definitions out of a header file. For templates the function definition must be available at the point where the template is instantiated. Similarly, if you want to enable the compiler to optimize function calls by inlining them the function definitions must be available to every compilation unit that invokes those functions. – IInspectable Sep 21 '13 at 23:26
2

The keyword is inline and not __inline.

inline is not a mere suggestion it is the best standard compliant way to include a function definition in a header file without breaking the one definition rule.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

Judging from the documentation in MSDN, I think it's a purely pragmatic thing, mostly for the benefit of C programs.

Since inline is a valid identifier name in C, another keyword is needed in C.

Personally, I would only consider using it if my source code could end up being shared between C and C++ programs (in Visual Studio, obviously).

Luis
  • 1,235
  • 1
  • 12
  • 12