Inline Implications of constexpr
Consider the following scenario: you have two versions of a function, one with an inline specifier and the other with a constexpr specifier. Both versions perform the same operation of doubling the input value. The question arises: does constexpr imply inline in the sense that non-constant arguments passed to the constexpr function would trigger inline expansion?
C 11 Standard Guarantee
According to the C 11 standard ([dcl.constexpr], §7.1.5/2), the answer is a resounding yes. "constexpr functions and constexpr constructors are implicitly inline (7.1.2)." This means that the compiler must treat constexpr functions as if they had the inline specifier declared.
Practical Implications
It's important to note that the inline specifier itself has minimal impact on whether a compiler actually inlines a function. It primarily affects the one definition rule, ensuring that there can be only one definition of an inline function.
However, in the case of constexpr functions, the compiler is obligated to follow the same rules for inline expansion. This means that if a constexpr function is invoked with non-constant arguments, the compiler should attempt to inline the function as if it had the inline specifier present.
Evolution of constexpr
While constexpr implied inline in C 11, the rules for constexpr functions have evolved in subsequent versions of the language. Today, constexpr functions can be far more complex and may not always be suitable for inline expansion. Nevertheless, the requirement for constexpr functions to be treated as implicitly inline remains in effect.
The above is the detailed content of Does constexpr Imply Inline for Non-Constant Arguments in C ?. For more information, please follow other related articles on the PHP Chinese website!