Can you inline static member functions?

17,275

Solution 1

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.

Solution 2

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:
17,275

Related videos on Youtube

John
Author by

John

for (int i = 10; i < 40; ++i) { learnSomething(); codeSomething(); writeSomething(); }

Updated on June 05, 2022

Comments

  • John
    John about 2 years

    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?

    • Kerrek SB
      Kerrek SB over 12 years
      As with any inlining, the answer is "it depends". What if you want to pass a function pointer to foo somewhere?
  • John
    John over 12 years
    They differ from free functions in one important respect: access to private members
  • Seth Carnegie
    Seth Carnegie over 12 years
    @John ah yes, that is an important difference.
  • Admin
    Admin over 8 years
    Doesn't a definition inside the class imply a request to inline anyway?