Question Details

No question body available.

Tags

c++

Answers (2)

February 13, 2026 Score: 12 Rep: 74,443 Quality: Expert Completeness: 60%

static inline isn't redundant, it's actively dangerous. Consider this:

static inline int add2(int i) { return i + 2; }
// possibly somewhere far away, where the difference isn't obvious:
inline int add2twice(int i) { return add2(add2(i)); }
// or possibly the use is in a template function definition

This looks harmless, but it is actually a violation of the ODR, probably undetected and therefore undefined behavior.

The linked question's top answer contains this:

The One Definition Rule requires that the body of the function definition is identical in every TU that contains it, with a longish definition of "identical".

One part of the definition of "identical" is that all names in the function definitions shall refer to the same entity in all definitions. And a static function of the same name in two different TUs is not the same entity.

This means that add2twice (which does not have internal linkage) has different definitions in different TUs, because the definitions refer to different add2s.

Therefore, do not make your inline functions static. Do not declare functions with internal linkage (i.e. static non-member functions or things in the unnamed namespace) in headers, ever. The same pretty much goes for variables, but there are a few exceptions for this.

Note that this applies to C++. C has subtly different semantics for inline and thus the advice may not apply. Be wary of C libraries using static inline in their headers: a good C library that's meant to be usable from C++ should use macros to switch between static inline and just inline for its inline functions.

February 13, 2026 Score: 3 Rep: 15,263 Quality: Medium Completeness: 80%

The GCC manual has a pretty explicit answer:

To declare a function inline, use the inline keyword in its declaration, like this:

   static inline int
   inc (int a)
   {
     return (a)++;
   }

[…]

The three types of inlining behave similarly in two important cases: when the inline keyword is used on a static function, like the example above, and when a function is first declared without using the inline keyword and then is defined with inline, like this:

   extern int inc (int a);
   inline int
   inc (int a)
   {
     return (*a)++;
   }

In both of these common cases, the program behaves the same as if you had not used the inline keyword, except for its speed.

(emphasis mine)

There you have it. The inline is redundant but the compiler will still consider the hint for inlining decisions.

Is inline still useful for optimization?

You wrote

[…] aside from the 'you should just inline this function' hint (that compilers will probably ignore anyway) […]

I don't think this idea that compilers don't rely on inline declarations for optimization hints is correct. When you look at the GCC source code for its inlining optimization for example, you will find many places where it checks the function declaration via its DECLDECLAREDINLINE_P macro and makes different decisions in its heuristics based on that result, e.g. applying different limits to acceptable function growth.

For the compiler it would be just as foolish to completely ignore the inlining hint as it would be to use it unconditionally.

The fact that it increases limits means that the inline declaration is more important for larger functions which themselves may be borderline as candidates for inlining, as well as smaller functions that themselves may have grown through inlining.