6

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?

21
  • 2
    inline is still valid and essential. The original intend is minor. Commented Jan 29, 2023 at 14:18
  • 3
    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. Commented Jan 29, 2023 at 14:20
  • 6
    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. Commented Jan 29, 2023 at 14:20
  • 5
    Aha, found it: Do compilers take inline as a hint?. @Eljay Commented Jan 29, 2023 at 14:25
  • 2
    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). Commented Jan 29, 2023 at 14:26

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

Note that modules give another (non-mandatory) meaning for inline which is more, er, in line with the original interpretation: inline functions (and many function templates, which continue to be similar in this area) cannot use TU-local entities in a module, precisely to make it a reasonable strategy to inline (in the assembly sense) only functions declared inline and treat the others as ABI boundaries.

Of course, using entities with internal linkage in inline functions has always been an ODR trap:

// f.hh static int x; inline int counter() {return x++;} 
// a.cc #include"f.hh" int wrapper() {return counter();} 
// main.cc #include"f.hh" int wrapper(); // from a.cc int main() {return 1 - wrapper() - counter();} 

Here the two definitions of counter (one per translation unit) refer to separate variables x, so it depends on the inlining decisions for each translation unit whether it appears that there are two counters (so main returns 1) or one (so main returns 0).

As such, C++20 also deprecates this situation even without modules, but that's consistent with the "new meaning" of the inline keyword as it just helps enforce that "all definitions are identical".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.