开发者

Code in header files will always be inlined?

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;
    };


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.


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


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.


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..


Class member function defined (as opposed to only declared) in the class' definition are implicitly inline. Other code in headers is not.

You can easily check this: Create a small C++ project with a header and two implementation files, define a function

void print(std::ostream& os)
{
  os << "Hello, world!\n";
}

in the header and include that header in both implementation files. The linker will now complain that the function is defined twice. Put an inline before the function definition and the error will disappear.

There are, however, a few more irregularities. For example, a constant definition will automatically get external linkage. Therefore, a

const int answer = 42;

in a header will not make the linker complain about multiple definitions of answer, while

int question;

will.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜