Are C++ class methods defined in the header always inlined?

前端 未结 6 684
醉酒成梦
醉酒成梦 2021-01-12 01:05

Edit: I\'ve restored the original title but really what I should have asked was this: \'How do C++ linkers handle class methods which have be

相关标签:
6条回答
  • 2021-01-12 01:15

    It doesn't have to be inlined, no; it's just like if you specified inline explicitly.

    When you write inline, you promise that this method won't be called from translation units where it isn't defined, and therefore, that it can have internal linkage (so the linker won't connect one object-file's reference to it to another object-file's definition of it). [This paragraph was wrong. I'm leaving it intact, just struck-out, so that the below comments will still make sense.]

    0 讨论(0)
  • 2021-01-12 01:16

    The inline keyword deals with c++ definition of a function. The compiler may inline object code where ever it wants.

    Functions defined inline (eg they use the inline keyword), create object code for the function in every compilation unit. Those functions are marked as special so the linker knows to only use one.

    See this answer for more specifics.

    0 讨论(0)
  • 2021-01-12 01:17

    7.1.2 Function specifiers

    A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares an inline function. The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions defined by 7.1.2 shall still be respected.

    So, the compiler is not required to actually 'inline' any function.

    However, the standard also says,

    An inline function with external linkage shall have the same address in all translation units.

    Member functions normally have external linkage (one exception is when the member function belongs to a 'local' class), so inline functions must have a unique address for cases where the address of the function is taken. In this case, the compiler will arrange for the linker to throw away all but one instance of a non-inlined copy of the function and fix-up all address references to the function to be to the one that's kept.

    0 讨论(0)
  • 2021-01-12 01:21

    There is no - single answer to this question. Compilers are smart beasts. You can specifically use the inline words if you want, but this doesn't mean that the compiler will actually inline the function.

    Inline is there to help the developer with optmization. It hints at the compiler that something should be inlined, but these hints are generally ignored nowadays, since compilers can do better at register assignment and deciding when to inline functions (in fact, a compiler can either inline or not inline a function at different times). Code generation on modern processors is far more complicated than on the more deterministic ones common when Ritchie was inventing C.

    What the word means now, in C++, is that it can have multiple identical definitions, and needs to be defined in every translation unit that uses it. (In other words, you need to make sure it can be inlined.) You can have an inline function in a header with no problems, and member functions defined in a class definition are automatically effectively inline.

    That said, I used to work with a greenhills compiler, and it actually obeyed my will more than it disobeyed it :).. It's up to the compiler, really.

    0 讨论(0)
  • 2021-01-12 01:22

    Section [9.3], Member functions, of the C++98 Standard states:

    A member function may be defined (8.4) in its class definition, in which case it is an inline member function (7.1.2).

    Thus, it has always been the case that marking member functions defined in the class definition explicitly inline is unnecessary.

    On the inline function specifier, the Standard states:

    A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares an inline function. The inline specifier indicates to the [C++ compiler] that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. [However, a C++ compiler] is not required to perform this inline substitution at the point of call;

    So, it is up to the compiler whether it will actually inline the definition of the function rather than call it via the usual function call mechanism.

    0 讨论(0)
  • 2021-01-12 01:28

    Nothing is always inlined (unless your compiler has an attribute or private keyword to force it to do so...at which point you're writing $(COMPILER)-flavored C++ rather than standard C++). Very long functions, recursive functions, and a few other things generally aren't inlined.

    The compiler can choose not to inline stuff if it determines that doing so will degrade performance, unreasonably increase the object file's size, or make things work incorrectly. Or if it's optimizing for size instead of speed. Or if you ask it not to. Or if it doesn't like your shirt. Or if it's feeling lazy today, cause it compiled too much last night. Or for any other reason. Or for no reason at all.

    0 讨论(0)
提交回复
热议问题