Namespaces
Variants
Views
Actions

inline specifier

From cppreference.com
 
 
C++ language
General topics
Preprocessor
Comments
Keywords
ASCII chart
Escape sequences
History of C++
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

[edit] Syntax

inline function_declaration

[edit] Description

The inline keyword is a hint given to the compiler to perform an optimization. The compiler has the freedom to ignore this request.

If the compiler inlines the function, it replaces every call of that function with the actual body (without generating a call). This avoids extra overhead created by the function call (placing data on stack and retrieving the result) but it may result in a larger executable as the code for the function has to be repeated multiple times. The result is similar to function-like macros

The function body must be visible in the current translation unit.

Class methods defined inside the class body are implicitly declared inline.

[edit] Example

inline int sum(int a, int b) 
{
    return (a + b);
}
 
int c = sum(1, 4);
// if the compiler inlines the function the compiled code will be the same as writing
int c = 1 + 4;