3

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.

alice
  • 2,547
  • 4
  • 24
  • 30
  • [This post](https://stackoverflow.com/questions/2579874/lifetime-of-a-const-string-literal-returned-by-a-function) may be of interest to you. – Cory Kramer Mar 01 '15 at 15:55
  • 1
    The literal `1` (or `'a'` or `false` etc) may or may not occupy some storage in the traditional sense of occupying storage. In other words, the compiler may well use some immediate value in the machine-code, that doesn't necessarily take up the same amount of space as the fundamental type - e.g. the value `1` may well be stored as a single byte, although an integer is 4 bytes. – Mats Petersson Mar 01 '15 at 16:01

1 Answers1

4

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.

rici
  • 234,347
  • 28
  • 237
  • 341
  • So when the standard refers to objects, they include string literals, but not other literals? – alice Mar 01 '15 at 16:51
  • @alice: a literal will be used to initialize an object if it is necessary to create a temporary object for the use of the literal. In the context of literals, when the standard refers to objects it is referring to the temporary object so created or the static string literal object, as the case may be, and not to the literal itself. – rici Mar 01 '15 at 16:56
  • The standard defines a prvalue to be "a temporary object or subobject thereof, or a value that is not associated with an object". In an expression `1 + 2`, `1` is a prvalue. But by what? Is it because it is a temporary object, or because it is a value not associated with an object? That was why I posted the question. – alice Mar 01 '15 at 17:26
  • It is a value that is not associated with an object. Why does it make a difference, though? Anyway, you could have just asked that in the first place :) – rici Mar 01 '15 at 17:28
  • @alice: the point is, it is not the case that "literals certainly occupy a region of storage". If a literal does not, it may is a value but it is not an object. – rici Mar 01 '15 at 17:30
  • There's also "An lvalue designates a function or an object", etc. So I wanted to be sure what exactly they call it an object, but then found those vague definitions. – alice Mar 01 '15 at 17:37