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

前端 未结 6 774
醉酒成梦
醉酒成梦 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: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.

提交回复
热议问题