本文介绍了“ODR 使用"是什么意思?某物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是在 另一个问题.

显然,类模板中的成员函数只有在 ODR 使用时才会被实例化.有人可以解释一下这到底是什么意思.维基百科关于单一定义规则 (ODR) 的文章 没有提到ODR 使用".

Apparently member functions in class templates are only instantiated if they are ODR-used.Could somebody explain what exactly that means. The wikipedia article on One Definition Rule (ODR) doesn't mention "ODR-use".

然而标准将其定义为

名称显示为潜在求值表达式的变量是 odr-used 除非它是满足以下要求的对象出现在常量表达式 (5.19) 和左值到右值中立即应用转换 (4.1).

在 [basic.def.odr] 中.

in [basic.def.odr].

显然这是错误的部分,整个段落包含对不同事物的多个定义.这可能与类模板成员函数相关:

Apparently this is the wrong part and the entire paragraph contains multiple definitions for different things. This might be the relevant one for class template member function:

一个非重载函数,其名称显示为潜在评估表达式或一组候选的成员函数,如果从 a 引用时由重载决议选择潜在评估的表达式,被 odr 使用,除非它是一个纯虚函数及其名称未明确限定.

但是我不明白,这个规则是如何跨多个编译单元工作的?如果我显式实例化一个类模板,是否所有成员函数都实例化了?

I do however not understand, how this rule works across multiple compilation units? Are all member functions instantiated if I explicitly instantiate a class template?

推荐答案

这只是一个任意定义,被标准用来指定何时必须为实体提供定义(如反对只是声明).标准不只是说使用",因为这可以根据不同的情况进行不同的解释语境.并且一些 ODR 使用并不真正对应于什么通常会与使用"相关联;例如,一个虚拟函数总是被 ODR 使用,除非它是纯函数,即使它不是实际上在程序中的任何地方调用.

It's just an arbitrary definition, used by the standard tospecify when you must provide a definition for an entity (asopposed to just a declaration). The standard doesn't say just"used", because this can be interpreted diversely depending oncontext. And some ODR-use doesn't really correspond to what onewould normally associate with "use"; for example, a virtualfunction is always ODR-used unless it is pure, even if it isn'tactually called anywhere in the program.

完整定义在 §3.2,第二段,尽管这包含对其他部分的引用以完成定义.

The full definition is in §3.2, second paragraph, although thiscontains references to other sections to complete thedefinition.

关于模板,使用 ODR 只是问题的一部分;另一部分是实例化.特别是,§14.7 涵盖当模板被实例化时.但两者是相关的:而§14.7.1(隐式实例化)中的文本相当长,基本原则是模板只会在以下情况下被实例化它被使用,在这种情况下,使用意味着 ODR 使用.因此,类模板的成员函数只会被实例化如果它被调用,或者它是虚拟的并且类本身是实例化.标准本身在许多方面都依赖于此地方:std::list<>::sort 对个体使用 <元素,但您可以在元素类型上实例化列表不支持<,只要你不调用sort

With regards to templates, ODR-used is only part of question;the other part is instantiation. In particular, §14.7 coverswhen a template is instantiated. But the two are related: whilethe text in §14.7.1 (implicit instantiation) is fairly long, thebasic principle is that a template will only be instantiated ifit is used, and in this context, used means ODR-used. Thus,a member function of a class template will only be instantiatedif it is called, or if it is virtual and the class itself isinstantiated. The standard itself counts on this in manyplaces: the std::list<>::sort uses < on the individualelements, but you can instantiate a list over an element typewhich doesn't support <, as long as you don't call sort onit.

这篇关于“ODR 使用"是什么意思?某物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 04:24