Inlined functions have a non-inlined copy

后端 未结 1 734
时光取名叫无心
时光取名叫无心 2021-01-14 03:57

In Agner Fog\'s Optimizing C++ manual he has a section \"Inlined functions have a non-inlined copy\" where he writes

Function inlining has the complic

相关标签:
1条回答
  • 2021-01-14 04:15

    The standard says that the full definition of an inline method needs to be visible in every translation unit that uses it:

    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). [...] 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.

    (7.1.2/4 in N4140)

    This does indeed make the example in your question ill-formed.

    This rule also includes every TU from whatever external module is linking your library. They would also need the full definition in C++ code, e.g. by defining the function in a header. So a compiler can safely omit any kind of "non-inlined copy" if the current translation does not need it.

    Concerning being certain the copy does not exist: The standard does not guarantee any optimization, so that is up to the compiler. Both with and without an additional static keyword.

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