c++inlineredeclaration

C++ inline redeclaration


Consider the following example (in the same translation unit):

inline void f();
void f() {}

What happens at the redeclaration of f? Is f still considered inline?

I looked in the standard for this situation but I only found the reverse of it in 10.1.6/6 [dcl.inline]:

[...] If the definition of a function or variable appears in a translation unit before its first declaration as inline, the program is ill-formed. [...]

I would like some references in the standard which specify what happens in this situation.

I saw this post, but it doesn't show an explicit reference in the standard. I'm inclined to believe there is no such reference.


Solution

  • What happens at the redeclaration of f? Is f still considered inline?

    Yes, f is still considered inline.

    The relevant sections of the standard pertaining to the semantics of the inline function specifier can be found in [dcl.fct.spec]:

    A function declaration ... 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 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 ([basic.def.odr]). [ 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. An inline function with external linkage shall have the same address in all translation units.

    And in [basic.def.odr]:

    Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program

    Also see an example in [dcl.stc]/8:

    void h();
    inline void h();                // external linkage
    
    inline void l();
    void l();                       // external linkage
    

    The interpretation of all that is: