Is the old meaning of the inline keyword deprecated in C++?
Asked Answered
E

2

5

On the cppreference page for the inline specifier, it says,

The inline specifier, when used in a function's decl-specifier-seq, declares the function to be an inline function.

An inline function has the following properties:
There may be more than one definition of an inline function in the program as long as each definition appears in a different translation unit and all definitions are identical.
...

Then, a bit down, it says,

The original intent of the inline keyword was to serve as an indicator to the optimizer that inline substitution of a function is preferred over function call. ...

Apart from this line, there is no reference to this use of inline. Is the latter meaning of inline still valid in the C++ standards? Or is the latter form deprecated over the former?

If the latter form is still valid, is it worth using it in modern compilers? I have heard that, even though it is the compiler that makes the decision about inlining, using the inline keyword pushes it a bit. Is this true? Should I use the inline keyword in my program for this case?

Eire answered 29/1, 2023 at 14:15 Comment(21)
inline is still valid and essential. The original intend is minor.Raul
I remember someone examining the source code of GCC and Clang and finding that they still somewhat respect the original meaning. Don't take my word for it though.Yuen
"Deprecated" has a specific meaning ("remains in the standard, but marked for possible future removal"). Since the latter meaning of inline was never in the standard (I think?), "deprecated" isn't the right word.Yuen
The original intent of inline is irrelevant. It was just a suggestion to the compiler. The compiler can do inlining with or without the inline keyword. It only affects how the function is linked.Redfield
It is "valid" in the sense that nothing in the C++ standard prohibits it. The C++ standard allows the C++ compiler to implement any optimization that has no observable effect. So a C++ compiler can inline anything, whether or not the keyword is used, as long as there are no observable effects. Whether or not C++ compilers still use they keyword as a hint to perform inlining can only be answered with empirical inner knowledge of most of today's compilers.Siliceous
Aha, found it: Do compilers take inline as a hint?. @RedfieldYuen
inline nowadays means, hey compiler whatever definition (implementation) of this function you find.. I promise they will all be the same, just link with whatever version you encounter. (Can be used to put implementation in a header file).Laterality
@PepijnKramer Check out the link.Yuen
@Yuen I refer to this : eel.is/c++draft/dcl.inline#6 and yes the standard still says the compiler may honor the classic inline hint. (eel.is/c++draft/dcl.inline#2)Laterality
Github returns 79 million code results with inline: github.com/search?l=C%2B%2B&q=inline&type=Code (compared to include that has 328M, it is not too small)Autochthonous
@PepijnKramer that not true, they must use same definition, there can be only one address. whether compiler inline it with or you create ODR-violation is another story.Prindle
@appleapple Yes there will only be one address but it is up to the programmer to make sure all definitions are the same. The compiler will pick one and use it everywhere (so if the provided definitions are not the same the resulting code will not be correct). Sorry if my initial wording was (and or is) a bit too loose. I am usually not the best language laywer ;)Laterality
@PepijnKramer or the compiler would use whatever it see in each translation unit and inline them, and call to the same name do different things in different translation unit. (UB)Prindle
or the compiler doesn't inline some calls and they can do different thing even in same translation unit. (again, it's UB so any other situation can happens)Prindle
@appleapple Those are good points too. So I guess it boils down to : when as a programmer you use the inline keyword, then make sure you have only one definition that the compiler can find (or all bets are off). Thanks for your feedback, my mental model of inline improved a bit today :)Laterality
@πάνταῥεῖ I don't think this should be tagged with language-lawyer. It's looking for a more practical answer (should I use inline to mean this...)Aitken
@appleapple: In C++, if you have a program with multiple translation units, and there is a function with external linkage which is inlined (either with the inline keyword or with other mechanisms) in every translation unit which calls the function, then the compiler (+linker) are guaranteed to produce an executable in which every reference to that function has the same address. As Pepijn said (correctly), the programmer must ensure that all the definitions are identical. That's it. The compiler will emit a "weak definition" in each TU, and the linker will choose one of those definitions.Gutturalize
That might be surprising for people more familiar with C's 'inline' semantics, which are quite different.Gutturalize
@Gutturalize yes, I mean, isn't it what I said in my first comment?Prindle
@appleapple: it's not an ODR violation to define the same inline function in multiple TUs. If that's what you meant to say, I did misunderstand. But I think it was not totally clear.Gutturalize
@Gutturalize it do seems not that clear, fwiw, that is reply to "link with whatever version you encounter" in this comment .Prindle
Y
6

The standard doesn't concern itself with how the assembly is generated, so "inlining" a function can't be mandated in it.

[dcl.inline]/2 words this as a recommendation:

... indicates to the implementation that inline substitution of the function body at the point of call is to be preferred ... An implementation is not required to perform this inline substitution ...

This blog post suggests that GCC and Clang do respect the inline hint to a certain degree.


Or the latter form is deprecated over the former?

As can be seen in [dcl.inline]/2, it's not.

I originally wanted to say that they can't deprecate it because they can't mandate it, but they failed to mark their recommendation as "note" (which would mean that it lacks the "standardizing power"), which looks like an editoral error to me.

Yuen answered 29/1, 2023 at 14:37 Comment(0)
A
1

The inline keyword makes it possible that all translation units will have access to the definition of a function, which will make it much easier for the compiler to inline the function instead of calling it. So, yes, it does facilitate inlining, though it doesn't (and has never) mandated it.

Should you use the inline keyword when you want the function to be inlined? Probably not, because you're probably wrong about how that will affect the performance. Note that inlining is still possible across translation units on major compilers if you turn on link time optimizations. So I recommend you do that and leave the rest to the compiler.

Also note that many compilers have extensions to more strongly recommend the compiler to inline a function, like always_inline in GCC. If you're going to use those, I recommend you profile your code before and after to see if you're helping or hurting the performance of your code.

Aitken answered 29/1, 2023 at 16:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.