简体   繁体   English

有关如何通过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

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不是intcv限定的。

  • 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是“ charunsigned 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.

相关问题 C ++ 11标准库中值和对象的示例? - C++11 example of a value and an object in the standard library? 临时对象的数据成员是C ++ 11中的xvalue吗? - Is a data member of a temporary object an xvalue in C++11? C ++ 11本地命名引用返回值(xvalue)? - C++11 local named references to return value (xvalue)? C ++ 11中的数据竞争,UB和计数器 - Data races, UB, and counters in C++11 C ++ 11:“decltype(1 + 2)”声明xvalue还是prvalue? - C++11: does “decltype(1+2)” declare an xvalue or a prvalue? C ++ 11标准是否要求通过常量unordered_container的两次迭代以相同的顺序访问元素? - Does the C++11 standard require that two iterations through a constant unordered_container visit elements in the same order? c ++ 11标准布局-使用相同的访问控制 - c++11 standard-layout - using same access control 寻找有关如何访问反序列化的谷物.xml文件中的数据的示例(Cereal,C ++ 11) - Looking for an example of how to access data in deserialized cereal .xml files (Cereal, C++11) 隐式声明(对象级别)成员函数如何根据ISO C ++ 11标准工作? - How do implicitly declared (object level) member functions work according to the ISO C++11 standard? 哪个“标准”是C ++ 11标准? - Which “standard” is the C++11 standard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM