是否允许C ++编译器/链接器删除未使用的方法? [英] Is a C++ compiler/linker allowed to remove unused methods?

查看:84
本文介绍了是否允许C ++编译器/链接器删除未使用的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否允许C ++编译器或链接器(通过任何C ++标准)删除未使用的方法? 似乎允许编译器删除未使用的静态函数,允许链接器删除未使用的函数.但是我没有发现有关类方法的信息.当该方法为虚拟方法时,这将变得非常有趣.

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.

推荐答案

C ++标准在更抽象的层次上起作用.实际上,它不需要C ++实现就可以由诸如编译器和链接器之类的单个工具组成.

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.

偶然地,我只是在机器上搜索了草稿PDF ,在整个1368页的文档中,链接器"一词的一个实例,甚至只是第22页脚注中有关字符集的一个实例.

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.

引用§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.

这篇关于是否允许C ++编译器/链接器删除未使用的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆