Is an int variable an object, according to the C++ Standard?

时光怂恿深爱的人放手 提交于 2020-08-04 18:47:10

问题


Below you'll find the definition of object in C++ Standard.

[intro.object]/1:

The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is created by a definition (6.1), by a new-expression (8.3.4), when implicitly changing the active member of a union (12.3), or when a temporary object is created (7.4, 15.2). An object occupies a region of storage in its period of construction (15.7), throughout its lifetime (6.8), and in its period of destruction (15.7). [ Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. —end note ] The properties of an object are determined when the object is created. An object can have a name (Clause 6). An object has a storage duration (6.7) which influences its lifetime (6.8). An object has a type (6.9). Some objects are polymorphic (13.3); the implementation generates information associated with each such object that makes it possible to determine that object’s type during program execution. For other objects, the interpretation of the values found therein is determined by the type of the expressions (Clause 8) used to access them.

I have a feeling that yes, an int variable can be considered an object in C++, notwithstanding this statement in the paragraph above:

An object occupies a region of storage in its period of construction (15.7), throughout its lifetime (6.8), and in its period of destruction (15.7).

There are several other statements in the Standard that seem to assume that the term object is restricted to class objects. Thus, for the sake of precision, I'm posting this question.


回答1:


Yes, an int is an object.

All the sentences in the quote you posted are valid and true for int variables.

Additionally, here are some examples that show how int makes sense for the quote you posted:


An object is created by a definition

From [basic.def]:

int a;                          // defines a

Suggesting that int is an object.


An object has a type

From [basic.types]:

There are two kinds of types: fundamental types and compound types.

int is a fundamental type. This again suggests that int is an object.




回答2:


Even though the standard doesn't enumerate everything that are objects, it can be deduced from various paragraphs. From [basic.types]/1

[basic.types] and the subclauses thereof impose requirements on implementations regarding the representation of types. There are two kinds of types: fundamental types and compound types. Types describe objects, references, or functions.

And [basic.types]/8

An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not cv void.

Since int is not a reference type nor a function type, it is deduced int can only be an object type, and a variable declared thus is an object.



来源:https://stackoverflow.com/questions/48026922/is-an-int-variable-an-object-according-to-the-c-standard

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