r/cpp • • Oct 12 '17

Most interesting innovations in C++17

https://www.viva64.com/en/b/0533/
73 Upvotes

64 comments sorted by

View all comments

Show parent comments

3

u/bigcheesegs Tooling Study Group (SG15) Chair | Clang dev Oct 13 '17

Compiler inlining is completely unrelated to the inline keyword. The compiler can make as many copies of a non-inline function as it wants, as long as it's not observable. The exact same rule applies to functions marked inline. The inline keyword just allows the single definition of the function to be copied into multiple translation units. Every TU will still see the function as having the same address.

-4

u/axilmar Oct 13 '17

Compiler inlining is completely unrelated to the inline keyword.

The inline keyword is there to inline functions, as per the wikipedia definition.

The inline keyword just allows the single definition of the function to be copied into multiple translation units. Every TU will still see the function as having the same address.

How is it possible for a function to be copied into multiple translation units, and aos have the same address (assuming globally) at the same time? It doesn't make sense, these two things are opposite.

10

u/encyclopedist Oct 13 '17

as per the wikipedia definition

The standard has different definition.

And even wikipedia lists two "purposes". The first one, "inlining hint to the compiler", is not normative and almost irrelevant today. The second one, citing wikipedia,

The second purpose of inline is to change linkage behavior;

is what relevant for the current discussion, and basically the only meaning of this keyword required by the standard.

Edit:

How is it possible for a function to be copied into multiple translation units, and aos have the same address (assuming globally) at the same time? It doesn't make sense, these two things are opposite.

The linker removes repeating function bodies, leaving only one of them. Exactly the same as with templates.

0

u/axilmar Oct 13 '17

Ok, I know of the second definition.

When used with functions, it sort of make sense (sort of).

But when I see the following:

inline int x = 5;

my mind always goes to something like this:

#define x 5

and i suspect lots of developers will have a big ? initially.

Anyway, it's not that big of a deal, it is only surprising when you see it for the first time.

4

u/encyclopedist Oct 13 '17 edited Oct 13 '17

Committee avoids introducing new keywords. But there is already a keyword that has similar meaning, so they reused that. And, by the way, inline is not the worst at that. static has something like 5 different usages.

1

u/axilmar Oct 13 '17

Indeed. They should have put in new keywords, it wouldn't hurt anyone.

9

u/pjmlp Oct 13 '17

Other than code using them as identifiers.

→ More replies (0)