Usually, virtual functions cannot be eliminated by the linker as dead code, since their addresses should appear in the vtable. However, if the vtable for struct C
was defined as dead code (which can happen if all the constructors are also dead code), then this last remaining link can also be removed.
Since the function is declared inline, this dead code removal optimization should not wait for link time; this can be done by the compiler. The Standard states (see Section 7.1.2):
The built-in function must be defined in each translation unit in which it is used , and must have exactly the same definition in each case (3.2). [Note: a call to a built-in function can be met before its definition appears in the translation block. - end note] If a function definition appears in the translation block before its first declaration as inline, the program is poorly formed. If a function with an external link is declared inline in one translation unit, it must be declared embedded in all translation units in which it appears; no diagnostics required. An inline
function with external communication must have the same address in all translation units. The local static
variable in the extern inline
function always refers to the same object. The string literal in the body of the extern inline
function is the same object in different translation units. [Note. The string literal contained in the default argument is not part of the built-in function just because the expression is used in the function call from this built-in function. - end note] The type defined inside the body of the extern inline
function is the same type in each translation unit.
If the compiler can determine that a function is never used in this translation unit, he knows that any translation unit that uses this function must contain its own identical definition and will generate code. Thus, it can skip code generation just as if it had no external connection at all.
Generating a warning is completely pointless, as there will be a large number of false positives (when the inline
function is used as odr and the code is generated in some other compilation unit).
source share