Take the 2-minute tour ×

I have a static member function which is merely syntactic sugar for me and I would like its body to appear in place of going through the motions of passing parameters to it. Will

inline static foo(int a) {return a & 0x00000040;}

be inlined just as it would if it was inline without being static?

share|improve this question
As with any inlining, the answer is "it depends". What if you want to pass a function pointer to foo somewhere? –  Kerrek SB Feb 11 '12 at 22:35
add comment

3 Answers

The compiler chooses what it wants to do so we can't say what it will choose to do. That said, the function being static will not prevent it from being inlined; static functions are basically free functions with a different naming style and access to the class' private members.

share|improve this answer
They differ from free functions in one important respect: access to private members –  John Feb 11 '12 at 22:44
@John ah yes, that is an important difference. –  Seth Carnegie Feb 11 '12 at 22:46
add comment

inline and static are orthogonal concepts - they don't relate to each other. It will be inlined as if it weren't static - i.e., it will be inlined if the compiler decides that it's worthwhile, which, in this case, is most certainly true (inlined it probably translates to just two assembly instructions).

On the other hand, static and inline are often used together for non-member functions; here static means "internal linkage", and it's used to avoid forcing the compiler to generate a non-inline version of the function to be accessible from other translation units (edit: this probably makes sense only in C, see comments) (although, if it decides that the function shouldn't be inlined for some reason, you'll get multiple copies of the function, one for each translation unit where it hasn't been inlined somewhere). To obtain this same effect on member functions (where static means "non-instance member") you have to resort to anonymous namespaces.

share|improve this answer
The compiler isn't forced to generate a non-inline version of an inline function (unless its address is taken) even if it has external linkage as the programmer must supply a definition in every translation unit in which it is used. –  Charles Bailey Feb 11 '12 at 22:50
@CharlesBailey: then I don't understand anymore why static and inline are seen so much together... :S –  Matteo Italia Feb 11 '12 at 22:52
I don't know. I've hardly come across static inline in the C++ that I've come worked on. It doesn't seem to add anything very useful. –  Charles Bailey Feb 11 '12 at 22:57
@CharlesBailey: I've seen it a lot in C code; I thought it was required for some difference in the ODR, but I checked C99 and inline functions are allowed to have many definitions. –  Matteo Italia Feb 11 '12 at 23:03
Well C is somewhat different because an inline function with external can have an inline definition and an external definition and you can't control which definition a call will use. I think that this means that you always need to have an external definition of an inline function with external linkage as well as an inline definition. The note in paragraph 8 of 6.7.4 (ISO/IEc 9899:TC3) seems to indicate as much although I admit it wasn't complete clear to me from the normative text. I think this means that static inline has some use in C. –  Charles Bailey Feb 11 '12 at 23:15
show 1 more comment

A static member method has no "this" parameter, and can therefore only access static member variables. It is distinct from whether the method is inlined or not. So the two are independent of each other. The compiler decides if a method is going to be inlined or not. Your use of the keyword is merely a hint to the compiler.

share|improve this answer
add comment

Your Answer


