Heiko Bloch Heiko Bloch - 1 year ago 149
C++ Question

Complete object or subobject?

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);

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.

Answer Source

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.).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download