What Does It Mean For a C++ Function To Be Inline?

It means one thing and one thing only: that the compiler will elide multiple definitions of the function.

A function normally cannot be defined multiple times (i.e. if you place a non-inline function definition into a header and then #include it into multiple compilation units you will receive a linker error). Marking the function definition as “inline” suppresses this error (the linker ensures that the Right Thing happens).

IT DOES NOT MEAN ANYTHING MORE!

Most significantly, it does NOT mean that the compiler will embed the compiled function into each call site. Whether that occurs is entirely up to the whims of the compiler, and typically the inline modifier does little or nothing to change the compiler’s mind. The compiler can–and does–inline functions that aren’t marked inline, and it can make function calls to functions that are marked inline.

Eliding multiple definitions is the thing to remember.

Leave a Comment