有关如何通过xvalue访问对象的值以引发UB的示例,如C ++ 11 Standard中的3.10 / 10中所述 [英] 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

查看:84
本文介绍了有关如何通过xvalue访问对象的值以引发UB的示例,如C ++ 11 Standard中的3.10 / 10中所述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

3.10 / 10

3.10/10


如果程序试图通过
a以外的其他值访问对象的存储值以下类型的行为是未定义的

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:


  • 对象的动态类型,

  • 对象动态类型的cv限定版本,

  • 与对象动态类型类似(定义见4.4)的类型

  • 一种类型,它是与对象的动态类型相对应的有符号或无符号类型,

  • 一种类型,它是与cv限定版本相对应的有符号或无符号类型对象的动态类型

  • 一种聚集或联合类型,其元素或非静态数据成员(包括
    递归地包括元素或子集合或
    包含的并集的非静态数据成员),

  • 一种类型(可能是经过cv限定)的基类类型对象的动态类型的e,

  • 一个char或未签名的char类型。

  • the dynamic type of the object,
  • a cv-qualified version of the dynamic type of the object,
  • a type similar (as defined in 4.4) to the dynamic type of the object,
  • 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,
  • 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,
  • a char or unsigned char type.


推荐答案

读者应该意识到,OP引用的段落是一个宗教问题,是g ++编译器有争议行为的基础。对本段的准确性或完整性提出质疑的任何答案(都不是),通常会在SO上被否决。

以下是UB的示例您引用的段落:

Here's an example of UB according to the paragraph you're citing:

struct X { int i; };

auto main() -> int
{
    X o{ 0 };
    return reinterpret_cast<int&>( o );
}

在C ++ 11§3.10/ 10中按顺序考虑每种可能性:

Considering each possibility in C++11 §3.10/10 in order:


  • 是对象的动态类型 o int

    ,动态类型为 X

int " cv 合格版本”类型X的动态类型?

X 不是 int cv 是否合格。

Is int perhaps “a cv-qualified version” of the dynamic type X?
No, X is not an int, cv-qualified or not.

int 类似于对象的动态类型的类型(定义见4.4)?

再次,。 4.4处理多级 cv 资格。

Is int “a type similar (as defined in 4.4) to the dynamic type of the object”?
Again, no. 4.4 deals with multi-level cv-qualification.

嗯,就是 int 一种类型,它是与对象的动态类型相对应的有符号或无符号类型?

,没有任何类型的有符号或无符号版本像 X

Well, is int “a type that is the signed or unsigned type corresponding to the dynamic type of the object”?
No, there are no signed or unsigned versions of a class type like X.

那么类型是与之对应的有符号或无符号类型呢?对象动态类型的cv限定版本?

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”?
No.

int 也许是集合或联合类型,其元素或非静态数据成员(包括递归地,元素或非静态数据成员)中包括上述类型之一

,也不是。

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)”?
No, not that either.

所以也许 int 是一种类型,它是对象动态类型的(可能具有cv限定的)基类类型 ;?

int 不能是基类。

So maybe int is “a type that is a (possibly cv-qualified) base class type of the dynamic type of the object”?
No, an int can’t be a base class.

最后是 int < a char 未签名的字符类型&rdquo ;?

Finally, is int “a char or unsigned char type”?
No.

这将耗尽所有可能性,并根据 that 段落单独证明,此代码具有未定义的行为。

And this exhausts all possibilities, proving that according to that paragraph in isolation, this code has Undefined Behavior.

但是,保证该代码可以通过该标准的另一部分工作(我想主要是为了实现C兼容性)。

However, this code is guaranteed to work by another part of the standard (I guess mainly for C compatibility).

因此,即使您引用的段落也不是100%好的完全独立于平台的形式。

So, the paragraph you cite isn't 100% good even for the completely platform-independent formal.

编辑: dyp在评论中问这与使用xvalue有关。一个xvalue是一个glvalue,因此可以只用一个xvalue代替左值表达式 o 。此类xvalue的示例是从函数(例如)返回的右值引用。从 std :: move

Edit: "dyp" asked in a comment how this relates to use of an xvalue. An xvalue is a glvalue, so one can just substitute an xvalue for the lvalue expression o. An example of such xvalue is an rvalue reference returned from a function, e.g. from 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 ) ) );
}

但这只是掩盖了要点。

这篇关于有关如何通过xvalue访问对象的值以引发UB的示例,如C ++ 11 Standard中的3.10 / 10中所述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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