1

Help me to correctly explain why literal is not an object in C++.

My explanation is: "Strictly speaking we can't think that literals are objects. Literals are prvalues excluding string literals. Often prvalues are values that is not associated with any object!"

This explanation consistent with the C++ standart (also see here). Maybe there is another explanation?

Community
  • 1
  • 1
Rodvi
  • 195
  • 1
  • 9
  • 4
    "Literals are not objects" Is a false statement. – George Dec 05 '16 at 11:22
  • @George But why? There is a claim in the standard "An entity is a value, object, reference, function, enumerator, type, class member, template, template specialization, namespace, parameter pack, or this." I think that this means that values are not objects. And literal is a fixed value. Am I wrong? – Rodvi Dec 05 '16 at 11:26
  • 1
    Yes, though admittadely that statement maybe slightly confusing, a value, ref, (instanced or static)class member, and this are all objects. In the same way that `this` is a class member. You should probably take a look at [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – George Dec 05 '16 at 11:34
  • 2
    Literals are elements of the program text. Objects are elements of the program execution environment. They exist in different universes. Some literals may have corresponding objects (e.g. string literals). Others don't. – n. m. could be an AI Dec 05 '16 at 11:50
  • @George Well, maybe I was wrong about literals. But you wrote that refs are objects. Why do you think so? This is contrary to Lippman's C++ Primer book from you list. And also this is contrary to [cppreference](http://en.cppreference.com/w/cpp/language/object) (sentence "The following entities are not objects: value, reference, function, enumerator, type, class member, bit-field, template, template specialization, namespace, parameter pack, and this.") I think these two sources are enough truthful. – Rodvi Dec 05 '16 at 11:59
  • 2
    String literals are objects – M.M Dec 05 '16 at 12:18
  • @n.m. Yes, I agree with you. Strictly speaking we can't think that literals are objects. Literals are prvalues excluding string literals. Often prvalues are values that is not associated with any object! – Rodvi Dec 05 '16 at 12:25

1 Answers1

0

A literal is a declarative object, however I think that maybe you are getting hung up on a distinction that primitives and objects. Primitives are objects and can be seen them as objects easily but due to there notation and lack of members, it can be easy to see why they could be perceived as not objects. Therefore it easy to see why it could be thought that a literal is not an object because literals usually define primitives (not many complex data structures are easy to define in literals).

However, a primitive is an object (no useful reason to not define them as objects) because it is a member of a type. Therefore since is just a static definition of an object they are also an object we just don't interact with them in the same way as other objects because they are static.

While the idea that a literal is not object probably won't cause any problems in code it can make OOP more complicated especially since the idea of OOP is that we should interact with object in programming because that is how we interact with everything outside of programming.

Garrigan Stafford
  • 1,331
  • 9
  • 20