“对ODR的使用”是什么意思,什么? [英] What does it mean to "ODR-use" something?

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

问题描述

这只是在另一个问题



显然,类模板中的成员函数只有在使用ODR时才被实例化。
有人可以解释一下究竟是什么意思。 维基百科的ODR文章没有提及 ODR使用



但是标准定义为


一个变量,其名称显示为潜在计算表达式
odr-used ,除非它是一个满足
出现在常量表达式(5.19)和左值到右值
转换4.1)立即应用。


编辑:显然这是错误的部分,整个段落包含多个定义的不同的东西。这可能是类模板成员函数的相关函数:


一个非重载函数,其名称显示为
,计算表达式或一组候选
函数的成员(如果从
潜在求值表达式引用时通过重载解析选择)是odr使用的,除非它是纯
虚拟函数,其名称未明确限定。


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

解决方案

这只是一个任意的定义, b $ b指定何时必须为实体提供一个定义(如
,而不是一个声明)。该标准不仅仅说
used,因为这可以根据
上下文来进行不同的解释。和一些ODR使用不真正对应的
通常与使用相关联;例如,虚拟
函数总是使用ODR,除非它是纯的,即使它不是
实际调用程序中的任何地方。



完整定义位于§3.2第二段,虽然此
包含对其他段的引用以完成
定义。



关于模板,使用ODR只是问题的一部分;
其他部分是实例化。特别地,当实例化模板时,§14.7涵盖
。但是两者是相关的:while
在§14.7.1(隐式实例化)中的文本是相当长的,
的基本原则是模板将只实例化如果
它被使用,并且在本上下文中,使用意味着使用ODR。因此,
类模板的成员函数只有在被调用时才被实例化
,或者如果它是虚拟的并且类本身是
被实例化。标准本身在许多
位置上: std :: list<> :: sort 使用< 对单个
元素,但你可以实例化一个元素类型的列表
不支持< 因为您不在
上调用 sort


This just came up in the context of another question.

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 ODR doesn't mention "ODR-use".

However the standard defines it as

A variable whose name appears as a potentially-evaluated expression is odr-used unless it is an object that satisfies the requirements for appearing in a constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) is immediately applied.

in [basic.def.odr].

Edit: 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 non-overloaded function whose name appears as a potentially-evaluated expression or a member of a set of candidate functions, if selected by overload resolution when referred to from a potentially-evaluated expression, is odr-used, unless it is a pure virtual function and its name is not explicitly qualified.

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?

解决方案

It's just an arbitrary definition, used by the standard to specify when you must provide a definition for an entity (as opposed to just a declaration). The standard doesn't say just "used", because this can be interpreted diversely depending on context. And some ODR-use doesn't really correspond to what one would normally associate with "use"; for example, a virtual function is always ODR-used unless it is pure, even if it isn't actually called anywhere in the program.

The full definition is in §3.2, second paragraph, although this contains references to other sections to complete the definition.

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

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

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