Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code in header files will always be inlined?

Tags:

c++

inline

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;
    };
like image 809
nabulke Avatar asked Jul 27 '10 10:07

nabulke


People also ask

When should a function be inlined?

Inline functions are commonly used when the function definitions are small, and the functions are called several times in a program. Using inline functions saves time to transfer the control of the program from the calling function to the definition of the called function.

Can all functions be inlined?

First, you cannot always inline, e.g. recursive functions might not be always inlinable (but a program containing a recursive definition of fact with just a printing of fact(8) could be inlined). Then, inlining is not always beneficial.

How do you know if a function is inlined?

If you need to make sure that function is inlined and OK to go with proprietary extension in MS VC++, check out the __forceinline declarator. The compiler will either inline the function or, if it falls into the list of documented special cases, you will get a warning - so you will know the inlining status.

Why is header inline static?

A static inline function is, in practice, likely (but not certain) to be inlined by some good optimizing compiler (e.g. by GCC when it is given -O2 ) at most of its call sites. It is defined in a header file, because it then could be inlined at most call sites (perhaps all of them).


2 Answers

Any function defined within the class (like your GetNumber example) rather than just declared is implicitly inline. What that means is that it's equivilent to using the inline keyword, so multiple inclusions of the header will not cause link errors due to multiple definitions of those functions.

Most modern compiler treat inline as a linkage command and nothing more. Some compilers provide stronger keywords such as CL's __forceinline which mean 'inline this if it's possible to do so'.

So you're both right and both wrong to a degree.

like image 182
JoeG Avatar answered Oct 02 '22 12:10

JoeG


Your pal is wrong, you're right.

Inlining do not depends on where the code is (header or not). After preprocessing there is no headers or non-headers. Whole unit is a single file, it contains all included stuff.

Try running gcc preprocessor then you will see:

gcc -E some_source_file_with_includes
like image 40
adf88 Avatar answered Oct 02 '22 11:10

adf88