[英]Example on how to access the value of an object through an xvalue, in order to provoke UB, as described in 3.10/10 in the C++11 Standard
3.10/10 3.10 / 10
If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:
如果程序尝试通过以下类型之一以外的glvalue访问对象的存储值,则行为未定义:
- the dynamic type of the object,
对象的动态类型,
- a cv-qualified version of the dynamic type of the object,
对象动态类型的cv限定版本,
- a type similar (as defined in 4.4) to the dynamic type of the object,
与对象的动态类型类似(定义见4.4)的类型,
- a type that is the signed or unsigned type corresponding to the dynamic type of the object,
类型是与对象的动态类型相对应的有符号或无符号类型,
- a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object,
一种类型,是与对象的动态类型的CV限定版本相对应的有符号或无符号类型,
- an aggregate or union type that includes one of the aforementioned types among its elements or nonstatic data members (including, recursively, an element or non-static data member of a subaggregate or contained union),
集合或联合类型,在其元素或非静态数据成员(递归包括子集合或包含的联合的元素或非静态数据成员)中包括上述类型之一,
- a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,
该类型是对象动态类型的(可能是cv限定的)基类类型,
- a char or unsigned char type.
字符或无符号字符类型。
Readers should be aware that the paragraph cited by the OP is a religious issue, as the basis of a disputed behavior of the g++ compiler. 读者应注意,OP引用的段落是一个宗教问题,是g ++编译器有争议行为的基础。 Any answer sowing doubt on the accuracy or completeness of this paragraph (and it's neither) will generally get downvoted on SO.
任何对本段的准确性或完整性表示怀疑的答案(两者都不是)通常会被否决。
Here's an example of UB according to the paragraph you're citing: 根据您引用的段落,这是UB的示例:
struct X { int i; };
auto main() -> int
{
X o{ 0 };
return reinterpret_cast<int&>( o );
}
Considering each possibility in C++11 §3.10/10 in order: 按顺序考虑C ++ 11§3.10/ 10中的每种可能性:
Is “the dynamic type of the object” o
an int
? 是“动态类型对象的”
o
一个int
?
No , the dynamic type is an X
. 不 ,动态类型是
X
Is int
perhaps “a cv -qualified version” of the dynamic type X? int
可能是动态类型X的“ cv限定版本”吗?
No , X
is not an int
, cv -qualified or not. 不 ,
X
不是int
或cv限定的。
Is int
“a type similar (as defined in 4.4) to the dynamic type of the object”? int
是“一种类型(如4.4中定义)与对象的动态类型类似”吗?
Again, no . 再次, 没有 。 4.4 deals with multi-level cv -qualification.
4.4处理多级简历资格。
Well, is int
“a type that is the signed or unsigned type corresponding to the dynamic type of the object”? 好吧,
int
是“与对象的动态类型相对应的有符号或无符号类型的类型”吗?
No , there are no signed or unsigned versions of a class type like X
. 不 ,没有像
X
这样的类类型的带符号或无符号版本。
So what about “a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object”? 那么,“与对象的动态类型的CV限定版本相对应的有符号或无符号类型的类型”呢?
No . 不行
Well, is int
perhaps “an aggregate or union type that includes one of the aforementioned types among its elements or nonstatic data members (including, recursively, an element or non-static data member of a subaggregate or contained union)”? 那么,
int
可能是“在其元素或非静态数据成员(包括递归地包括子聚合或包含的并集的元素或非静态数据成员)中包括上述类型之一的聚合或联合类型”?
No , not that either. 不 ,也不是。
So maybe int
is “a type that is a (possibly cv-qualified) base class type of the dynamic type of the object”? 因此,也许
int
是“一种类型,它是对象的动态类型的(可能是cv限定的)基类类型”?
No , an int
can't be a base class. 不 ,一个
int
不能作为基类。
Finally, is int
“a char
or unsigned char
type”? 最后,
int
是“ char
或unsigned char
类型”吗?
No . 不行
And this exhausts all possibilities, proving that according to that paragraph in isolation, this code has Undefined Behavior. 这就用尽了所有可能性,证明根据该段单独的内容,该代码具有未定义的行为。
However, this code is guaranteed to work by another part of the standard (I guess mainly for C compatibility). 但是,保证该代码可以在该标准的另一部分工作(我想主要是为了实现C兼容性)。
So, the paragraph you cite isn't 100% good even for the completely platform-independent formal. 因此,即使对于完全独立于平台的形式,您引用的段落也不是100%好的。
Edit : "dyp" asked in a comment how this relates to use of an xvalue. 编辑 :“ dyp”在评论中询问这与使用xvalue有何关系。 An xvalue is a glvalue, so one can just substitute an xvalue for the lvalue expression
o
. 一个xvalue是一个glvalue,因此可以只用一个xvalue代替lvalue表达式
o
。 An example of such xvalue is an rvalue reference returned from a function, eg from std::move
: 这样的xvalue的一个示例是从函数(例如,从
std::move
返回的右值引用:
#include <utility>
using std::move;
struct X { int i; };
template< class T >
auto ref( T&& r ) -> T& { return r; }
auto main() -> int
{
X o{ 0 };
return reinterpret_cast<int&>( ref( move( o ) ) );
}
All this does is however to mask the essentials. 但是,所有这些都掩盖了要点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.