Aussie AI
constexpr functions vs inline functions
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
constexpr
functions vs inline
functions
A lot of the same value in terms of optimization can be had by making
a function just inline
rather than constexpr
.
Note that you can use both, but officially constexpr
for functions implies inline
on the function as well.
Is constexpr
any better than just inline
?
If you pass a constant argument to a small inline
function, then the expansion
of the function body will trigger lots of constant propagation optimizations,
effectively evaluating most of it at compile-time,
which is almost the same as constexpr
.
constexpr
is supposed to be more formal in guaranteeing that the
result of a function is a compile-time constant,
and the compiler is honor-bound to do “compile-time function evaluation”
to get the constant return value.
Also, a constexpr
function is more officially usable as a compile-time constant,
so that you can use an expression with a constexpr
function's return value
in various places where C++ needs a constant (e.g. an array size declaration, some template
situations, etc.).
An inline
function is also supposed to be optimized at run-time for non-constant arguments,
and constexpr
functions are implicitly inline
functions.
The code generation requirements of dynamic inlining are often more advanced that
constant expression evaluation.
Also, the limitations on how a constexpr
function can be structured
are a lot easier to code than the unrestricted nature of an inline
function body.
However, as a practical matter, the compile-time evaluation of expressions and
the code generation for inlined expressions have a lot of overlap,
so I expect C++ compilers will mostly try to do both on every type of function.
The inline
keyword also serves a weird secondary purpose, by guaranteeing that
there's only one copy of the function.
This means we can include header files with the full definition of that inline
function
anywhere we like, without getting a compiler error at link-time about multiple definitions.
But this isn't a performance optimization,
and the linker feature of inline
is almost the opposite of what we want in making
a function inline
, because we don't
want a real function to be called at all.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |