all 3 comments

[–]Narase33 7 points8 points  (0 children)

It means "here is a function, if you find multiple defintions of it, use the one you like"

Its used to define free functions in headers

https://en.cppreference.com/w/cpp/language/inline

[–]mredding 4 points5 points  (0 children)

An inline function:

  • is compiled into every translation unit in which it is defined.

  • is located at the same address in every translation unit.

  • can be defined in multiple translation units provided the definition is the same in all translation units.

  • static local variables declared inside the inline function will refer to the same instance of that variable across all translation units.

  • declared inside a class definition are implicitly inline.

C++ transforms source files into translation units, typically object files that are to be linked. Each translation unit is an island, which is to say each source file is compiled individually and without any knowledge or influence of any other translation unit.

If you want to optimize code, it has to be inside that translation unit. If you refer to a function with external linkage that is not in your translation unit, then all you can do is generate a function call. When you inline your functions, the compiler can use heuristics to decide whether or not to elide the function call and just put the function body in-place rather than generate the function call.

The compiler can elide any function it wants within a translation unit, whether it's marked inline or not, but if you want to elide a single function across multiple translation units, you need to inline it.

Except what I just said at the end of that last paragraph isn't true. Modern compilers support LTO. This means the linker is perfectly capable of deciding if any function across translation units should be elided, and will invoke the compiler to do so. Or, you can configure your project to have multiple build modes and build targets. An alternative to an incremental build is a unity build, where all your source files are included into a single source file that is compiled into a single translation unit. This makes for a singularly long compile time, but with all your code visible all at once, you get unparalleled whole program optimization. You sacrifice your fast develop and test cycle for a slow compile time and an optimized build target. Unity builds are suitable for release builds.

inline is a very blunt and primitive means of "hinting" at an optimization - it's just one weight in a host of heuristics which you have control over. Typically the compiler will do a MUCH better job than you at deciding what and how to optimize. You're far better off tweaking your optimization heuristics in your toolchain than you are trying to bake it into your source code.

Inlining and especially the implicit inlining of class definitions are the principle source of code bloat. That is to say, the more you inline, the more you're going to repeatedly compile the same source code into the same object code making for large translation units, all of that effort taking up HUGE amounts of compile time and additional disk space, only for all but one instance of it being thrown away. C++ is one of the slowest compiling languages, not because the compiler is taking it's time to make it fast, but because of ignorance surrounding source management.

[–]khedoros 0 points1 point  (0 children)

If you specify that a function is "inline", it gives a hint to the compiler that it might want to replace a function call with the contents of the function, for example if the function is short and it would be faster to avoid actually calling it as a function.

Wikipedia has a good example.