jjzjj

c++ - 出于对象替换的目的,对象的 "name"究竟是由什么构成的?

coder 2024-02-22 原文

根据[basic.life]/8,

If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object, if:

  • the storage for the new object exactly overlays the storage location which the original object occupied, and
  • the new object is of the same type as the original object (ignoring the top-level cv-qualifiers), and
  • the type of the original object is not const-qualified, and, if a class type, does not contain any non-static data member whose type is const-qualified or a reference type, and
  • the original object was a most derived object (4.5) of type T and the new object is a most derived object of type T (that is, they are not base class subobjects).

... [ Note: If these conditions are not met, a pointer to the new object can be obtained from a pointer that represents the address of its storage by calling std::launder (21.6). — end note ]

该标准包含一个示例,演示当存在 const 子对象时,“原始对象的名称”无法引用新对象,并且使用该名称会导致 UB。它位于 [intro.object]/2:

Objects can contain other objects, called subobjects. A subobject can be a member subobject (12.2), a base class subobject (Clause 13), or an array element. An object that is not a subobject of any other object is called a complete object. If an object is created in storage associated with a member subobject or array element e (which may or may not be within its lifetime), the created object is a subobject of e’s containing object if:

  • the lifetime of e’s containing object has begun and not ended, and
  • the storage for the new object exactly overlays the storage location associated with e, and
  • the new object is of the same type as e (ignoring cv-qualification).

[ Note: If the subobject contains a reference member or a const subobject, the name of the original subobject cannot be used to access the new object (6.8). — end note ] [ Example:

struct X { const int n; };
union U { X x; float f; };
void tong() {
  U u = {{ 1 }};
  u.f = 5.f;                          // OK, creates new subobject of u (12.3)
  X *p = new (&u.x) X {2};            // OK, creates new subobject of u
  assert(p->n == 2);                  // OK
  assert(*std::launder(&u.x.n) == 2); // OK
  assert(u.x.n == 2);                 // undefined behavior, u.x does not name new subobject
}

然而,在我看来 [basic.life]/8 没有在 u.x.n 定义的行为上给出左值到右值的转换这一事实是无关紧要的,因为它是给定的[expr.ref]/4.2 定义的行为,其中有以下关于类成员访问表达式 E1.E2 的说明:

If E2 is a non-static data member and the type of E1 is “cq1 vq1 X”, and the type of E2 is “cq2 vq2 T”, the expression designates the named member of the object designated by the first expression. ...

我对此的理解是,表达式 u.x 产生一个左值,它引用任何对象 u< 的="">current x 子对象 当前指的是。因为,根据 [intro.object]/2,在 u.x 位置创建新的 X 对象会导致新的 X 对象要真正成为 u 的子对象,对 u.x.n 执行左值到右值的转换应该是明确定义的。

如果我们假设此示例中的 UB 反射(reflect)了标准的意图,看来我们必须将 [basic.life]/8 解读为说 尽管某些表达式可能出现的事实访问新对象(在这种情况下,由于 [expr.ref]/4.2),如果他们尝试使用原始对象的“名称”进行访问,它们仍然是 UB。 (或者,实际上,编译器可能假定“名称”继续引用原始对象,因此不会重新读取 const 成员的值。)

不过,通常情况下,我不认为 u.x 算作“命名”uX 子对象,因为我认为子对象没有名字。因此,[basic.life]/8 似乎是在说 UB 发生在某些特定情况下,但没有准确解释这些情况是什么。

因此,我的问题是:

  1. 我说 [basic.life]/8 导致这个例子包含 UB 而不是简单地没有给它定义的行为,我说得对吗?
  2. [basic.life]/8 是否对哪些案例给出了 UB 的精确说明?
  3. 是否应该改写标准以更清楚地说明 [basic.life]/8 何时会导致 UB(即,何时需要 std::launder)?

最佳答案

My reading of this is that the expression u.x yields an lvalue referring to the current x subobject of whatever object u currently refers to.

这是真的。您误解的是“当前 x 子对象”的意思。

当您执行 new (&u.x) X {2} 时,会在 u.x 的地址创建一个新对象。然而,标准中没有任何地方说这个对象被命名为 xu.x。是的,它是 u 的子对象,但它没有名称。标准在任何地方都没有说新创建的子对象具有该名称,或与此相关的任何名称。

事实上,如果你说的是真的,[basic.life]/8 和 launder 根本就不需要存在,因为一个对象在另一个对象的覆盖存储中总是可以访问的通过旧对象的名称。

Normally, though, I would not think that u.x counts as "naming" the X subobject of u, because I think that subobjects do not have names.

我不知道你是怎么得出这个结论的,因为你引用了规范中明确规定成员子对象可以有名称的部分:

the expression designates the named member of the object

添加了强调。这清楚地向我暗示成员子对象有一个名称,表达式 u.x 指定该特定成员子对象的名称(不仅仅是该地址中适当类型的任何成员子对象)。

也就是说,正如u特指u声明所声明的对象一样,u.x特指x - 由声明 u 声明的对象的命名成员。

关于c++ - 出于对象替换的目的,对象的 "name"究竟是由什么构成的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54613240/

有关c++ - 出于对象替换的目的,对象的 "name"究竟是由什么构成的?的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  3. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  4. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  5. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  6. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  7. ruby-on-rails - Ruby on Rails : . 常量化 : wrong constant name error? - 2

    我正在使用这个:4.times{|i|assert_not_equal("content#{i+2}".constantize,object.first_content)}我之前声明过局部变量content1content2content3content4content5我得到的错误NameError:wrongconstantnamecontent2这个错误是什么意思?我很确定我想要content2=\ 最佳答案 你必须用一个大字母来调用ruby​​常量:Content2而不是content2。Aconstantnamestart

  8. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  9. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  10. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

随机推荐