Can a C ++ compiler / linker delete unused methods?

Is the C ++ compiler or linker (by any C ++ standard) deleting an unused method? Compilers seem to be allowed to remove unused static functions, linkers are allowed to remove unused functions. But I did not find any information on how this looks for class methods. When the method is virtual, it becomes really interesting.

+4
source share
4 answers

The C ++ standard works at a more abstract level. This does not require a C ++ implementation to actually consist of separate tools, such as a compiler and linker.

By the way, I was just looking for a draft PDF on my machine, and there is only one instance of the word โ€œlinkerโ€ in all 1368-pages, and even that is only in the footnote on page 22 about character sets.

What the standard is really talking about is the so-called as-if rule .

Citation ยง1.9:

(...) necessary to emulate (only) the observed behavior of an abstract machine, as described below.

The footnote for this proposal further states:

This provision is sometimes called the โ€œas isโ€ rule, since the implementation can freely ignore any requirements of this International Standard , as long as the result is as if the requirements were met , as far as it can be determined from the observed program behavior.

, . , "as-if" , .

+1

.

, , - . , "" , .

, , - ( ).

, , , (, , vtable). , , , , , .

+5

++ , , , -, , . , . , , .

(, ..) , - (, -) , .

, , , - . , , , , . , .

, , . , โ€‹โ€‹. , , ( , , ), .

+2

, . , this, , . .

Compilers could test how vtable instances are used to detect unused virtual members, but to track small benefits. GCC did not do this a few years ago.

Keyword volatileor export retains class variables and functions / methods when optimizations are removed.

+1
source

All Articles