alice
alice

Reputation: 2625

Are literals objects by the standard terminology?

The standard defines:

An object is a region of storage. [ Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. —end note ] An object is created by a definition (3.1), by a new-expression (5.3.4) or by the implementation (12.2) when needed.

(12.2 is about temporary objects)

Literals certainly occupy a region of storage. I'm not sure the last sentence of the quote is the necessary condition to be an object. Literals are neither created by a definition nor by a new-expression, but all literals except string literals are temporary objects, right? That leads to the strange conclusion that string literals are not objects while all other literals are objects. This feels wrong.

Upvotes: 3

Views: 155

Answers (1)

rici
rici

Reputation: 241861

Aside from character string literals, there is no guarantee that a literal occupies storage. Small literals might be incorporated directly in machine instructions with direct operands, or even (as is frequently the case with 0) be computed or permanently present in a machine register.

If it is necessary to create a temporary, then the phrase "by the implementation as needed" applies.

As for string literals, §2.13.5 says (¶16): "Evaluating a string-literal results in a string literal object with static storage duration…". Perhaps a reference to that section should be added to the list cited in the OP.

Upvotes: 4

Related Questions