tags:

views:

192

answers:

7

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;
    };
+1  A: 

That's not true, only code in member functions will be inlined (should the compiler decide to do so) if specified within the class declaration. It must allow the members to be defined though, so they might be declared as the equivalent of C's static (which will allow it to be used within the compilation unit, but cannot be linked to other object files), which will put one version in each object file. Try it yourself, you'll notice that unless you specify the inline keyword for anything outside the class declaration, you'll get duplicates.

roe
+1  A: 

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.

JLWarlow
+1: didn't know about _forceinline
nabulke
+2  A: 

This depends on what you mean by "inlining". If something is defined in a header file, it will be compiled separately into each compilation unit that includes it. Whether any calls to the function will be inlined will be up to the compiler.

Matti Virkkunen
+5  A: 

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
adf88
Inlining does depend on where the code is. If the source for a function is declared in another translation unit it can't be inlined because the compiler can't see the definition, unless you have a compiler/linker that supports whole program optimization.
Joe Gauterin
+1: liked your comment about the whole unit after preprocessing
nabulke
I wrote about HEADER FILES, there is no difference if the definition is inside a header or not. Read carefully.
adf88
+2  A: 
sbi
+2  A: 

Actually both are correct. The way you guys meant is bit different. (I guess)

From the C++ standard doc for inline function,

  1. A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares an inline function.

2. A function defined within a class definition is an inline function.

So as your colleague said, it is indeed inline function .

But the code substitution for the inline function instead of the normal function call is dependent on the compiler. (I hope this is what YOU mean) Even if the compiler doesn't substitute it is still an inline function.

Hope it clears your concern..

liaK
Inline function doesn't have to be inlined, non inline function can be inlined. Compiler decide. Besides, you are talking about specifics of the example not related to the question.
adf88
@adf88, that's what i meant. The code substitution is decided by the compiler but the function is indeed an inline function. All inline functions need not be code substituted by the compiler..
liaK
The question in not about if the funcion is inline function (in C++ language manner). It is about if the function will be inlined by the compiler.
adf88
@adf88, Ahh.. I'll explain in your terms. A fuction defined within a class definition is inlined always (in c++ language manner) but whether it will be code substituted is decided by the compiler.. Am I clear now atleast ?? :)
liaK
+1: you answer did clear up some things for me. You got it right, that we both talked about different things. Thanks
nabulke
You right, but this is not answer to the question. The question is about header files, not about classes.
adf88
@ adf88, So what about the example he gave? Isn't class or just header files? C'mon.. It is a valid answer IMO.. In his opinion too..
liaK
If so, the question is terribly formed. "He says that code defined in header files will always be inlined by the compiler" - that's very clear to me. There is no word about classes. You are saying that this is not about header files. Then is it really answer to that question? Maybe the author didn't meant header files, but that's the question he asked.
adf88
The same case is with "inlined by the compiler" and "inline function". Even if he meant about inline functions, he asked about inlineing by the compiler.
adf88
I have to admit that my using of the phrase 'inlining/inline' in my question is not 100 % clear and might be inconsistent. But you live and learn...
nabulke
+4  A: 

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.

Joe Gauterin