Home > Article > Backend Development > How Does `constexpr` Interact with `inline` in C 11?
In C 11, the introduction of constexpr as a type qualifier for functions raised questions about its relationship with inline, an existing mechanism for inlining functions. This article examines the implications of constexpr and how it interacts with inline.
The C 11 standard explicitly states that "constexpr functions and constexpr constructors are implicitly inline". This means that using constexpr as a function specifier implicitly sets the inline specifier as well. Therefore, if a function is declared constexpr, it will behave as if it had the inline specifier.
This implies that if a non-constant argument is passed to a constexpr function, the compiler will still attempt to inline the function as if it were declared inline. This is because constexpr functions are required to meet certain criteria that make them suitable for inlining, such as being simple and not recursive.
However, it's crucial to note that the inline specifier has a more significant impact on the one definition rule (ODR) than on inlining itself. Functions with different inline qualifiers can have multiple definitions, while constexpr functions, like inline functions, must have a single definition.
While constexpr functions implicitly imply inline, the inline specifier has limited impact on inlining decisions. The compiler ultimately determines whether to inline a function based on various factors, including its size and complexity. Constexpr functions were initially designed to be suitable for inlining, but subsequent relaxations have allowed for more complex constexpr functions.
The above is the detailed content of How Does `constexpr` Interact with `inline` in C 11?. For more information, please follow other related articles on the PHP Chinese website!