Why are C++ inline functions in the header?

132,279

Solution 1

The definition of an inline function doesn't have to be in a header file but, because of the one definition rule (ODR) 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.

Solution 2

There are two ways to look at it:

  1. Inline functions are defined 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)

  2. functions defined 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.

Solution 3

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

Solution 4

The c++ inline keyword is misleading, it doesn't mean "inline this function". If a function is defined as inline, it simply means that it can be defined multiple times as long as all definitions are equal. It's perfectly legal for a function marked inline to be a real function that is called instead of getting code inlined at the point where it's called.

Defining a function in a header file is needed for templates, since e.g. a templated class isn't really a class, it's a template for a class which you can make multiple variations of. In order for the compiler to be able to e.g. make a Foo<int>::bar() function when you use the Foo template to create a Foo class, the actual definition of Foo<T>::bar() must be visible.

Solution 5

The reason is that the compiler has to actually see the definition in order to be able to drop it in in place of the call.

Remember that C and C++ use a very simplistic compilation model, where the compiler always only sees one translation unit at a time. (This fails for export, which is the main reason only one vendor actually implemented it.)

Share:
132,279
thecoshman
Author by

thecoshman

I think I might be starting to like Java; but not quite as much as I like that giant pink bunny in the corner of my room.

Updated on July 14, 2020

Comments

  • thecoshman
    thecoshman almost 4 years

    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 were 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:\Users\Me\Documents\Visual Studio 2012\Projects\inline\Debug\inline.exe 
    : fatal error LNK1120: 1 unresolved externals
    
  • thecoshman
    thecoshman over 13 years
    But doesn't the compiler compile the .cpp file, which includes the .h files... so that when it does compile a .cpp file it has both the deceleration as well as the source files. Other header files pulled in are just their so the compiler can 'trust' that those functions do exist and will be implemented in some other source file
  • sbi
    sbi over 13 years
    This is actually a much better answer than mine, +1 from me!
  • thecoshman
    thecoshman over 13 years
    Oh I see! yes, its not for the class it self the uses the inline function, its other code that makes use of the inline functions. They only see the header file for the class that is being inlined!
  • CB Bailey
    CB Bailey over 13 years
    I disagree with this answer, it's not a C++ compiler limit; it's purely how the language rules are specified. The language rules allow a simple compilation model but they don't prohibit alternative implementations.
  • CB Bailey
    CB Bailey over 13 years
    @thecoshman: There are two distinctions. Source file vs header file. By convention, a header file usually refers to a source file that isn't that basis for a translation unit but is only #included from other source files. Then there is declaration vs definition. You can have declarations or definitions of functions in either header files or 'normal' source files. I'm afraid I'm not sure what you are asking in your comment.
  • thecoshman
    thecoshman over 13 years
    don't worry, I get why it is now... though I am not sure who really answered this one. A combination of yours and @Xanatos's answer explained it for me.
  • sbi
    sbi over 13 years
    I agree with @Charles. In fact there are compilers that inline functions across translation units, so this definitely isn't due to compiler limitations.
  • sbi
    sbi over 13 years
    And since it is a template for a class, it isn't called a template class, but a class template.
  • thecoshman
    thecoshman over 13 years
    Whilst this answer does seem to have some technical errors, it did help me see how the compiler works with header files and such.
  • Thomas Edleson
    Thomas Edleson over 13 years
    The first paragraph is completely right (and I wish I could emphasize "misleading"), but I don't see the need for the non sequitur into templates.
  • Chuck Walbourn
    Chuck Walbourn over 9 years
    This is generally not going to result in the function actually being inlined unless you are using Whole Program Optimization (WPO).
  • thecoshman
    thecoshman almost 6 years
    eh, some nice info perhaps, but doesn't really attempt to explain why. Maybe you do, but just aren't making it clear.
  • Pablo Ariel
    Pablo Ariel almost 6 years
    The following statement is not true: "Important : If we define a function inside class it will become Inline automatically" Not even if you write "inline" in the declaration/definition you can be sure that it's being in fact inlined. Not even for templates. Maybe you meant that the compiler automatically assumes the "inline" keyword, yet doesn't have to follow and what I've noticed is that in most cases it doesn't inline such in-header definitions, not even for simple constexpr functions with basic arithmetics.
  • Saurabh Raoot
    Saurabh Raoot over 5 years
    Hey thanks for comments ...Below are lines from Thinking in C++ micc.unifi.it/bertini/download/programmazione/… Page 400 .. Please check .. Please upvote if you agree. Thanks ..... Inlines inside classes To define an inline function, you must ordinarily precede the function definition with the inline keyword. However, this is not necessary inside a class definition. Any function you define inside a class definition is automatically an inline.
  • Pablo Ariel
    Pablo Ariel over 5 years
    The authors of that book can claim what they want, because they write books and not code. This is something I had to analyze deeply in order to make my portable 3d demos fit in less than 64kb by avoiding inlined code as much as possible. Programming is about facts and not religion, so it doesn't really matter if some "god programmer" said it in a book if it doesn't represent what happens in practice. And most C++ books out there are a collection of bad advice, in which from time to time you get to find some neat trick to add to your repertory.
  • Saurabh Raoot
    Saurabh Raoot over 5 years
    Hey @PabloAriel Thanks... Please analyse and let me know .. I am ok to update this answer as per analysis
  • Pablo Ariel
    Pablo Ariel over 5 years
    Ok, then do the analysis. I already did and you're the one who is OK with what some random guy said in a book. And I'm OK with that, because it means you're not a competitor.
  • Keith M
    Keith M over 5 years
    Some compilers will use it as a hint that the function can probably be inlined, but indeed, it is not guaranteed to be inlined just because you declare it inline (nor does not-declaring it inline guarantee that it won't be inlined).
  • Kenn Sebesta
    Kenn Sebesta about 2 years
    This is the best description, but I agree with @ThomasEdleson that the segue isn't helpful and in fact is what keeps me from upvoting.