inline specifier

< cpp‎ | language

Declares an inline function.


[edit] Syntax

inline function_declaration (1)
class { function_definition }; (2)
constexpr function_declaration (3) (since C++11)
1) A function at global scope can be declared inline using the keyword inline
2) A function defined entirely inside a class/struct/union definition, whether it's a member function or a non-member friend function, is always inline.
3) A function declared constexpr is always inline.

[edit] Description

An inline function is a function with the following properties:

1) 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. For example, an inline function may be defined in a header file that is #include'd in multiple source files.
2) The definition of an inline function must be present in the translation unit where it is called (not necessarily before the point of call).
3) An inline function with external linkage (e.g. not declared static) has the following additional properties:
1) It must be declared inline in every translation unit.
2) It has the same address in every translation unit.
3) Function-local static objects in all function definitions are shared across all translation units (they all refer to the same object defined in one translation unit)
4) Types defined in all function definitions are also the same in all translation units.
5) String literals in all function definitions are shared (they are all the same string literal defined in just one translation unit)
(until C++17)

The intent of the inline keyword is to serve as an indicator to the optimizer that inline substitution of the function is preferred over function call, that is, instead of executing the call CPU instruction to transfer control to the function body, a copy of the function body is executed without generating the call. This avoids extra overhead created by the function call (copying the arguments and retrieving the result) but it may result in a larger executable as the code for the function has to be repeated multiple times.

Since this meaning of the keyword inline is non-binding, compilers are free to use inline substitution for any function that's not marked inline, and are free to generate function calls to any function marked inline. Those choices do not change the rules regarding multiple definitions and shared statics listed above.

[edit] Notes

If an inline function is defined differently in different translation units, the behavior is undefined.

The inline specifier cannot be used with a function declaration at block scope (inside another function)

The inline specifier cannot re-declare a function that was already defined in the translation unit as non-inline.

The implicitly-generated member functions and any member function declared as deleted are inline just like any other function defined inside a class definition.

If an inline function is declared in different translation units, the accumulated sets of default arguments must be the same at the end of each translation unit.

In C, inline functions do not have to be declared inline in every translation unit (at most one may be non-inline or extern inline), the function definitions do not have to be identical (but the behavior of the program must not depend on which one is called), and the function-local statics are distinct between different definitions of the same function.

[edit] Example

// header file
#ifndef EXAMPLE_H
#define EXAMPLE_H
// function included in multiple source files must be inline
inline int sum(int a, int b) 
    return a + b;
// source file #2
#include "example.h"
int a()
    return sum(1, 2);
// source file #1
#include "example.h"
int b()
    return sum(3, 4);

[edit] See Also

C documentation for inline