Inline (C and C++)

By whom? WP:OR.

← Previous revision Revision as of 14:21, 22 April 2026
Line 7: Line 7:
In the [[C (programming language)|C]] and [[C++]] [[programming language]]s, an '''inline function''' is one qualified with the [[Keyword (computer programming)|keyword]] inline; this serves two purposes:
In the [[C (programming language)|C]] and [[C++]] [[programming language]]s, an '''inline function''' is one qualified with the [[Keyword (computer programming)|keyword]] inline; this serves two purposes:
# It serves as a [[compiler directive]] that suggests (but does not require) that the [[compiler]] substitute the body of the function inline by performing [[inline expansion]], i.e. by inserting the function code at the address of each function call, thereby saving the overhead of a function call. In this respect it is analogous to the register [[storage class specifier]], which similarly provides an optimization hint.{{cite journal |title=The New C: Inline Functions |url=http://www.drdobbs.com/the-new-c-inline-functions/184401540 |first=Randy |last=Meyers |date=July 1, 2002}}
# It serves as a [[compiler directive]] that suggests (but does not require) that the [[compiler]] substitute the body of the function inline by performing [[inline expansion]], i.e. by inserting the function code at the address of each function call, thereby saving the overhead of a function call. In this respect it is analogous to the register [[storage class specifier]], which similarly provides an optimization hint.{{cite journal |title=The New C: Inline Functions |url=http://www.drdobbs.com/the-new-c-inline-functions/184401540 |first=Randy |last=Meyers |date=July 1, 2002}}
# The second purpose of inline is to change linkage behavior; the details of this are complicated. This is necessary due to the C/C++ separate compilation + linkage model, specifically because the definition (body) of the function must be duplicated in all [[Translation unit (programming)|translation unit]]s where it is used, to allow inlining during ''compiling'', which, if the function has external [[Linkage (software)|linkage]], causes a collision during ''linking'' (it violates uniqueness of external symbols). C and C++ (and dialects such as GNU C and Visual C++) resolve this in different ways.
# The second purpose of inline is to change linkage behavior. This is necessary due to the C/C++ separate compilation + linkage model, specifically because the definition (body) of the function must be duplicated in all [[Translation unit (programming)|translation unit]]s where it is used, to allow inlining during ''compiling'', which, if the function has external [[Linkage (software)|linkage]], causes a collision during ''linking'' (it violates uniqueness of external symbols). C and C++ (and dialects such as GNU C and Visual C++) resolve this in different ways.


== Example ==
== Example ==