Alex Rivera | Logout

Code in header files will always be inlined?

Asked 2010-07-27T10:49:20.030
17

I just had a discussion with a coworker concerning code in header files:

He says that code defined in header files will always be inlined by the compiler (like the code from the function GetNumber() in my example header). I say it will be inlined sometimes, whenever the compiler decides to do so. So which one of us has to bring a cake to work for telling filthy lies? Or maybe we are both wrong...?

MyClass.hpp

   class MyClass
    {
    public:
    MyClass();
    ~MyClass();

    int GetNumber() const 
    {
     //...; 
     return m_number;
    };

    private:
    int m_number;
    };
Edit
Report

1 Answer

1

The compiler decides. Even using _inline only tells the compiler you prefer inline code, but the compiler's cost/benefit analyser can decide otherwise.

You can use _forceinline if you're using Microsoft C++ to make the code inline, but that could result in larger binaries.

answered 2010-07-27T10:52:42.790

Your Answer