inline specifier - cppreference.com (2024)

C++ language

General topics
Flow control
Conditional execution statements
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

Declarations

The inline specifier, when used in a function's decl-specifier-seq, declares the function to be an inline function.

A function defined entirely inside a class/struct/union definition, whether it's a member function or a non-member friend function, is implicitly an inline function unless it is attached to a named module(since C++20).

A function declared constexpr or consteval(since C++20) on its first declaration is implicitly an inline function.

A deleted function is implicitly an inline function: its (deleted) definition can appear in more than one translation unit.

(since C++11)

The inline specifier, when used in a decl-specifier-seq of a variable with static storage duration (static class member or namespace-scope variable), declares the variable to be an inline variable.

A static data member declared constexpr on its first declaration is implicitly an inline variable.

(since C++17)

Contents

  • 1 Explanation
  • 2 Notes
  • 3 Keywords
  • 4 Example
  • 5 Defect reports
  • 6 See also

[edit] Explanation

An inline function or inline variable(since C++17) has the following properties:

  • The definition of an inline function or variable(since C++17) must be reachable in the translation unit where it is accessed (not necessarily before the point of access).
  • An inline function or variable(since C++17) with external linkage (e.g. not declared static) has the following additional properties:
  • There may be more than one definition of an inline function or variable(since C++17) in the program as long as each definition appears in a different translation unit and (for non-static inline functions and variables(since C++17)) all definitions are identical. For example, an inline function or an inline variable(since C++17) may be defined in a header file that is included in multiple source files.
  • It must be declared inline in every translation unit.
  • It has the same address in every translation unit.

In an inline function,

  • 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).
  • Types defined in all function definitions are also the same in all translation units.

Inline const variables at namespace scope have external linkage by default (unlike the non-inline non-volatile const-qualified variables).

(since C++17)

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, that is, instead of executing the function call CPU instruction to transfer control to the function body, a copy of the function body is executed without generating the call. This avoids overhead created by the function call (passing 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 inline substitution is unobservable in the standard semantics, 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 optimization choices do not change the rules regarding multiple definitions and shared statics listed above.

Because the meaning of the keyword inline for functions came to mean "multiple definitions are permitted" rather than "inlining is preferred" since C++98, that meaning was extended to variables.

(since C++17)

[edit] Notes

If an inline function or variable(since C++17) with external linkage is defined differently in different translation units, the program is ill-formed, no diagnostic required.

The inline specifier cannot be used with a function or variable(since C++17) declaration at block scope (inside another function).

The inline specifier cannot re-declare a function or variable(since C++17) that was already defined in the translation unit as non-inline.

The implicitly-generated member functions and any member function declared as defaulted on its first declaration 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 is unspecified if it depends on which one is called), and the function-local statics are distinct between different definitions of the same function.

See static data members for additional rules about inline static members.

Inline variables eliminate the main obstacle to packaging C++ code as header-only libraries.

(since C++17)
Feature-test macroValueStdFeature
__cpp_inline_variables201606L(C++17)Inline variables

[edit] Keywords

inline

[edit] Example

Header "example.h":

#ifndef EXAMPLE_H#define EXAMPLE_H#include <atomic>// function included in multiple source files must be inlineinline int sum(int a, int b){ return a + b;}// variable with external linkage included in multiple source files must be inlineinline std::atomic<int> counter(0);#endif

Source file #1:

#include "example.h"int a(){ ++counter; return sum(1, 2);}

Source file #2:

#include "example.h"int b(){ ++counter; return sum(3, 4);}

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
CWG 281C++98a friend function declaration could use the inline specifier
even if the friended function is not an inline function
prohibit such uses
CWG 317C++98a function could be declared inline even if it has a non-inline
definition in the same translation unit before the declaration
the program is ill-
formed in this case
CWG 765C++98a type defined in an inline function might
be different in different translation units
such types are the same
in all translation units
CWG 1823C++98string literals in all definitions of an inline
function were shared across all translation units
the requirement is removed due to
consistency and implementations
CWG 2531C++17a static data member could be implicitly inline even if
it was not declared constexpr on its first declaration
it is not implicitly
inline in this case

[edit] See also

C documentation for inline

inline specifier - cppreference.com (2024)
Top Articles
Polyamory
MANA Price Prediction: Can Decentraland Coin Reach $100?
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 6468

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.