1

With one being the 'optimization' effect, and the other being the effect related the the ODR.

To me those two seem like completely unrelated things, so I'm having a hard time understanding why the same keyword is used for both.

user2520938
  • 411
  • 3
  • 15
  • 2
    The only impact on ODR is that inline functions are not subject to ODR (since their definitions have to be repeated in every translation unit that uses them). This is only a side-effect of inlining, not a purpose in itself. – Frédéric Hamidi May 26 '14 at 08:36
  • 2
    @FrédéricHamidi One could argue it is the other way around, since actual inclining isn't guaranteed, but the ODR thing is. – juanchopanza May 26 '14 at 08:42
  • +1 and it's mostly used as a linker directive than as a compiler directive. – legends2k May 26 '14 at 08:43
  • @legends2k, I don't think the linker alone can be responsible for inlining the function. It needs support from the compiler as well. – Frédéric Hamidi May 26 '14 at 08:44
  • may address your question: http://stackoverflow.com/questions/5057021/why-are-c-inline-functions-in-the-header – Cengiz Kandemir May 26 '14 at 08:44
  • @FrédéricHamidi: I don't think so; the most upvoted answer on _inline_ here @ SO says [extern, static, inline are linkage directives, used almost exclusively by the linker, not the compiler.](http://stackoverflow.com/a/1759575/183120) – legends2k May 26 '14 at 11:29

3 Answers3

2

compare with the many uses of const, static and auto (et al)

the C++ Committee is positively allergic to reserving new keywords.

sp2danny
  • 7,488
  • 3
  • 31
  • 53
1

Techinically, I don't think it actually changes the ODR - you are only supposed to provide one definition (this definition can occur multiple times, but it should be the same). I'm a bit too lazy to look up the exact words of the specification, but I remember from a previous discussion on this subject that "you must not have a different declaration elsewhere" (in other words, the actual code itself should be the same every time).

As to why: Because the original usage is still what inline intends - it's just that compilers these days are smart enough to figure out when it's a good idea and when it isn't a good idea, to actually inline a function.

The key is that we need some way to tell the compiler and linker that "this function is the same function, even if you see it multiple times". One could invent a new keyword, but the more keywords that are used by the compiler, the fewer words we as programmers have available. And of course, ancient code would still be using the old keyword, so it still would need to be supported. I can't see much benefit in adding a new keyword that does the same as the existing one.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Yes I kind of get what you saying, but this is just confusing. It makes people (or me at least) feel like there's something they're not quite getting. That there's a relation or similarity between the different effects that they don't see. Or that the one effect logically implies the other in some way. Same with the different uses of the static keyword. In my opinion it's just confusing and annoying. – user2520938 May 26 '14 at 09:03
0

What is the relation between the two different effects of the inline keyword?

In a formal sense, when you inline a function there's no point of having external linkage for it and hence it also makes the function linkage interal (side effect).

why the same keyword is used for both

It is a typical case of where the side effect took over the mainline scenario i.e. today inline is just an opinion about the function by the programmer and the compiler has the final say on actually inlining the call; however the usage is more towards making a function have internal linkage when defining it in a header file.

See here and here.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222