Why are C++ inline functions in the header?

NB This is not a question about how to use inline functions or how they work, more why they are done the way they are.

The declaration of a class member function does not need to define a function as inline , it is only the actual implementation of the function. For example, in the header file:

struct foo{
    void bar(); // no need to define this as inline
}

So why does the inline implementation of a classes function have to be in the header file? Why can't I put the inline function the .cpp file? If I where to try to put the inline definition in the .cpp file I would get an error along the lines of:

error LNK2019: unresolved external symbol 
"public: void __thiscall foo::bar(void)"
(?bar@foo@@QAEXXZ) referenced in function _main 
1>C:UsersMeDocumentsVisual Studio 2012ProjectsinlineDebuginline.exe 
: fatal error LNK1120: 1 unresolved externals

The definition of an inline function doesn't have to be in a header file but, because of the one definition rule for inline functions, an identical definition for the function must exist in every translation unit that uses it.

The easiest way to achieve this is by putting the definition in a header file.

If you want to put the definition of a function in a single source file then you shouldn't declare it inline . A function not declared inline does not mean that the compiler cannot inline the function.

Whether you should declare a function inline or not is usually a choice that you should make based on which version of the one definition rules it makes most sense for you to follow; adding inline and then being restricted by the subsequent constraints makes little sense.


There are two ways to look at it:

  • Inline functions are declared in the header because, in order to inline a function call, the compiler must be able to see the function body. For a naive compiler to do that, the function body must be in the same translation unit as the call. (A modern compiler can optimize across translation units, and so a function call may be inlined even though the function definition is in a separate translation unit, but these optimizations are expensive, aren't always enabled, and weren't always supported by the compiler)

  • functions declared in the header must be marked inline because otherwise, every translation unit which includes the header will contain a definition of the function, and the linker will complain about multiple definitions (a violation of the One Definition Rule). The inline keyword suppresses this, allowing multiple translation units to contain (identical) definitions.

  • The two explanations really boil down to the fact that the inline keyword doesn't exactly do what you'd expect.

    A C++ compiler is free to apply the inlining optimization (replace a function call with the body of the called function, saving the call overhead) any time it likes, as long as it doesn't alter the observable behavior of the program.

    The inline keyword makes it easier for the compiler to apply this optimization, by allowing the function definition to be visible in multiple translation units, but using the keyword doesn't mean the compiler has to inline the function, and not using the keyword doesn't forbid the compiler from inlining the function.


    This is a limit of the C++ compiler. If you put the function in the header, all the cpp files where it can be inlined can see the "source" of your function and the inlining can be done by the compiler. Otherwhise the inlining would have to be done by the linker (each cpp file is compiled in an obj file separately). The problem is that it would be much more difficult to do it in the linker. A similar problem exists with "template" classes/functions. They need to be instantiated by the compiler, because the linker would have problem instantiating (creating a specialized version of) them. Some newer compiler/linker can do a "two pass" compilation/linking where the compiler does a first pass, then the linker does its work and call the compiler to resolve unresolved things (inline/templates...)

    链接地址: http://www.djcxy.com/p/29796.html

    上一篇: 如何在现代C ++中实现经典的排序算法?

    下一篇: 为什么头文件中的C ++内联函数?