Complete object or subobject?

不羁的心 提交于 2019-12-10 23:56:16

问题


The C++14 Standard says:

A subobject can be a member subobject, a base class subobject, or an array element. An object that is not subobject of any other object is called a complete object. (§1.8(2))

It is not obvious to me whether the 'can be' is meant as an implicit 'if and only if'. To provide an example, in the snippet below, is r a reference to a complete object or to a subobject?

#include <iostream>
int main(){
  int i=2;
  unsigned char & r=reinterpret_cast<unsigned char&>(i);
  std::cout<<(int)r<<"\n";  
}

As r refers to an unsigned char in the object representation , r should refer to an object:

The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, ... (§3.9(4))


edits: Could you please be very clear about what the first byte of i is: 1) no object at all, 2) a complete object, 3) a subobject

There are only these three possibilities.


回答1:


The sentence is defining the term subobject as being one of: a member subobject, a base class subobject, or an array element.

Your snippet has nothing to do with subobjects. r is a reference, not an object. Moreover, it doesn't even refer to an object, it simply aliases the first byte of i.

From [intro.object]:

An object is created by a definition (3.1), by a new-expression (5.3.4), when implicitly changing the active member of a union (9.3), or when a temporary object is created (4.4, 12.2).

i is an object created by a definition. As int is not a class or array type, it has no subobjects. The object representation, the underlying array of unsigned char that constitutes the storage of i, is not an object - it's not created in any of those contexts described above. The wording of the definition object representation is the subject of core issue 1701 (h/t T.C.).



来源:https://stackoverflow.com/questions/40108184/complete-object-or-subobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!