Moving inline methods from a header file to a .cpp files

前端 未结 6 1091

I have the following class defined in a foo.h header file

class Foo {

public:
    inline int Method();

};

inline int Foo::Method() { // Imple         


        
6条回答
  •  生来不讨喜
    2020-12-21 08:43

    Keyword inline is redundant in the class. It is implied if you have a function body.

    In the implementation file it is also fairly redundant.

    The only use of it is if you define a free function in a header (or a member function outside the class, but in the header) to avoid multiple bodies.

    Optimization-wise on mist modern compilers it's even more redundant, they inline anything in sight without question anyway, or ignore your keyword at will.

    The inline usage must be consistent! From 7.1.2p4:

    An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case (3.2). [ Note: A call to the inline function may be encountered before its definition appears in the translation unit. —end note ] If the definition of a function appears in a translation unit before its first declaration as inline, the program is ill-formed. If a function with external linkage is declared inline in one translation unit, it shall be declared inline in all translation units in which it appears; no diagnostic is required. ...

提交回复
热议问题