简体   繁体   中英

Is a C++ compiler/linker allowed to remove unused methods?

Is a C++ compiler or linker (by any C++ standard) allowed to remove an unused method? Compilers seem to be allowed to remove unused static functions, linkers are allowed to remove unused functions. But i have found no information what it looks like for methods of classes. When the method is virtual this gets really interesting.

Yes.

If the method is unused, then there is no way to tell it has been removed - so a linker can do so. Note that taking the address of a method may well count as "using" the method - not just actually calling it.

Linkers are quite likely to remove non-virtual member functions (it's easy and saves space).

They could remove unused virtual functions, but the compiler would have to add a lot of information about which virtual functions it was calling so that the linker could remove unused ones (and possibly compact the vtable). In practise, I don't think linkers do this because the gain is probably small and the amount of development effort required rather large.

Since the C++ standard says nothing about this, it is probably more accurate to say it does not prevent removal of unused member functions, than to say it permits their removal. But it is also equally valid to say it does not require their removal. That, incidentally, is true for all functions.

If programs in the build chain (compiler, linker, etc) can detect that any symbol (eg static or non-static member function) is not being used, it can safely remove them.

The harder practical problem is probably detecting that a non-static member function is not being called. Even more so, if it is virtual, since the static type of an object is what determines which override of a virtual function is called. Detecting such cases is not impossible in principle, but would require a fair amount of analysis.

In the end it will come down to quality of implementation of the build chain, since the standard requires nothing in particular. That comes down to whether the vendor chooses to implement such optimisations. And, since this is not a behaviour that many developers actually seek (the most likely people to seek it will have a premature optimisation fetish) not many would.

The C++ standard works on a more abstract level. It does not require a C++ implementation to actually be composed of individual tools like a compiler and a linker.

Incidentally, I just searched the draft PDF on my machine, and there is only one instance of the word "linker" in the entire 1368-pages document, and even that one is just in a footnote on page 22 about character sets.

What the standard actually does talk about is the so-called "as-if" rule .

Citing §1.9:

(...) conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.

In a footnote for that sentence, it further says:

This provision is sometimes called the “as-if” rule, because an implementation is free to disregard any requirement of this International Standard as long as the result is as if the requirement had been obeyed , as far as can be determined from the observable behavior of the program.

If a function is not used anywhere in your program in any way, then it cannot have any impact on observable behaviour. The "as-if" rule alone thus gives a compiler or linker full liberty to remove it from the executable result.

Class methods get inlined when they're defined in class definition. When they're implemented separately, they're simple functions with a hidden first parameter this which becomes the pointer to the instance which call this function as member of his class. There's no problem in removing an unused class method.

Compilers could check how the vtable of instances is used to detect unused virtual members but that's a lot of tracking for small benefits. A few years ago, GCC didn't do this.

Keyword volatile or exporting saves variables & functions/class methods from removing optimizations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM