user2520938
user2520938

Reputation: 421

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

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.

Upvotes: 1

Views: 80

Answers (3)

Mats Petersson
Mats Petersson

Reputation: 129354

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.

Upvotes: 1

legends2k
legends2k

Reputation: 32904

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.

Upvotes: 0

sp2danny
sp2danny

Reputation: 7644

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

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

Upvotes: 2

Related Questions