加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 编译器是否会排除未使用的模板代码?

发布时间:2020-12-16 03:34:33 所属栏目:百科 来源:网络整理
导读:当您使用具有多种方法的模板(如vector)并编译代码时,编译器是否会丢弃未使用方法中的代码? 解决方法 除非使用模板,否则不会实例化模板,因此实际上没有要丢弃的代码. 标准说(14.7.1 / 10) An implementation shall not implicitly instantiate a function te
当您使用具有多种方法的模板(如vector)并编译代码时,编译器是否会丢弃未使用方法中的代码?

解决方法

除非使用模板,否则不会实例化模板,因此实际上没有要丢弃的代码.

标准说(14.7.1 / 10)

An implementation shall not implicitly instantiate a function template,a member template,a non-virtual member function,a member class,or a static data member of a class template that does not require instantiation. It is unspecified whether or not an implementation implicitly instantiates a virtual member function of a class template if the virtual member function would not otherwise be instantiated. The use of a template specialization in a default argument shall not cause the template to be implicitly instantiated except that a class template may be instantiated where its complete type is needed to determine the correctness of the default argument. The use of a default argument in a function call causes specializations in the default argument to be implicitly instantiated.

因此,如果您可以避免将模板的成员函数设置为虚拟,则编译器将不会为它们生成任何代码(如果编译器足够智能,那么它也可能适用于虚函数).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读