Aussie AI
inline function limitations
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
inline function limitations
The inline
specifier is wonderful when it works.
A very important point to note about inline
functions is that the inline
specifier,
by itself, is not enough to guarantee that inline code will be generated. The other requirement is that the compiler must know the function body code, where the function is called.
Hence, an inline
keyword in a function prototype declaration is not enough. The executable statements
inside the function’s definition (i.e., the function body) must be available to the C++ compiler.
Otherwise, how
is the compiler to know what inline code to expand a function call into?
I guess in theory the C++ compiler could maintain a huge database of all the functions in your source code,
or scan through all the CPP files to find it,
and that would be amazing, but we're not there yet.
In practice, the compiler will only inline functions where it has seen the function body
within the current C++ source file or an included header file.
This requirement imposes two restrictions on the use of inline
functions:
1. Member functions declared as inline
should include the function body inside the
same header file as the class declaration. This can be achieved by placing the function
body of a member function inside the class declaration. For a more readable style when
there are many inline
member functions, the class declaration can declare the
function prototypes, and then provide the inline
function definitions immediately after
it, in the same header file. This restriction ensures that whenever the class declaration is
included as a header file, the member function body is available for inlining.
2. Non-member inline
functions must be defined before they are used within a source
file, preferably by placing the inline
functions in a header file. Placing inline
functions at the top of a source file allows the inlining of any function calls later in the
same source file, but calls to the functions from a different source file cannot be inlined
by the compiler unless the inline
function definition is placed in a header file.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |