Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 475
João Afonso Avatar asked Dec 29 '17 18:12

João Afonso


1 Answers

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.

like image 147
Vittorio Romeo Avatar answered Sep 30 '22 15:09

Vittorio Romeo