Inline qualifier stems from prototype or definition?

后端 未结 4 2281
鱼传尺愫
鱼传尺愫 2021-02-13 18:55

I\'m not quite sure about this in the standards. Say I have three files like this:

foo.h

#include 

inline void foo();

         


        
相关标签:
4条回答
  • 2021-02-13 19:18

    Odds are that your clang uses C99 inline semantics. In C99, if one of your functions doesn't use "inline" or does include "extern", then the definition is an "external definition", which can only appear once in the program. See inline in C99.

    In C++, your program is fine. In Clang SVN, this bug has been fixed and your program should work fine.

    0 讨论(0)
  • 2021-02-13 19:21

    You have to use inline in both places.

    0 讨论(0)
  • 2021-02-13 19:30

    I believe that the intention of the standard has always been to allow a function to be made inline by having at least one declaration including the inline specifier but there was some uncertainty about when it was too late to add the first inline declaration. Was after the definition too late, or after the first call?

    My reasoning for this is two fold, first the examples in 7.1.1, although non-normative and principally about storage class specifiers, suggest that inline isn't required on every declaration.

    Secondly this defect report DR 317 from 2001 (voted in in 2005) which adds the "If the definition of a function appears in a translation unit before its first declaration as inline, the program is ill-formed." sentence. It is clear from the conversation that it was intented that inline isn't required on every declaration, specifically in the case of a member function defined explicitly inline but outside of the class body where the original declaration didn't have an explicit inline.

    (That defect report also contains my mantra that inline is "more than a hint".)

    Of course, as soon as a function with external linkage is an inline function due to one or more declarations including the inline specifier in one translation unit it must be declared inline in all translation units according to the rest of paragraph 7.1.2/4.

    In the example in the question I believe that the intention is that foo is an inline function and that it is valid code although the normative text of the standard seems to me to be less clear than it could be.

    0 讨论(0)
  • 2021-02-13 19:31

    C++0X draft N3225 says in 7.1.2 Function specifiers:

    • clause 2: A function declaration with an inline specifier declares an inline function
    • clause 4: 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.

    So, to me, it looks like gcc is right & clang wrong, but there's still a (slim) chance that things were (are?) different in C++03..

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