Inline functions and external linkage

前端 未结 4 1276
太阳男子
太阳男子 2020-12-11 04:45

In this answer https://stackoverflow.com/a/4193698/738811 it\'s written that \"Inline functions by default have external linkage\". However it\'s not possible by default to

4条回答
  •  情话喂你
    2020-12-11 05:08

    The linkage of a name has nothing to do with where or how it is defined, just with where the name may be used to refer to a particular object or function.

    Declaring a function inline does not force it to be inlined; it just relaxes the One Definition Rule to allow a definition in each translation unit in which it's used (and require one in each translation unit in which it's called), to make it easier to inline. It doesn't prevent a non-inline version being generated, if the compiler decides not to inline a particular call to it, or if you take the address of it.

    So "external linkage" and "inline" are not exclusive; "external linkage" means that the function may be referred to in any translation unit, and "inline" means that it must be defined in any translation unit that calls it.

提交回复
热议问题