-8

First of all, let me say that I am sorry to ask couple of different things on a single SO question but they are all related and I don't know how to separate them.

In the book, From Mathematics to Generic Programming, Alexander Stepanov makes following definitions.

Defnition 10.1. A datum is a sequence of bits.

01000001 is an example of a datum.

Defnition 10.2. A value is a datum together with its interpretation.

So 65 and character 'A' are two different values that can share the same datum if we are to implement them as such in memory.

Defnition 10.3. A value type is a set of values sharing a common interpretation.

Up until this point, things are not really about programming. These definitions are more of a philosophical nature. For example, Leibniz could have created a taxonomy of all knowledge using something similar.

Things get related to computers here.

Defnition 10.4. An object is a collection of bits in memory that contain a value of a given value type.

Defnition 10.5. An object type is a uniform method of storing and retrieving values of a given value type from a particular object when given its address.

So an object is a way to represent things in memory and object type is a way to work with these representation in a programming language. Compiler tracks the relation between object and corresponding object type so I can express abstractions in my program. In light of these definitions, here are couple of questions.

Take a look at following code.

int b = 5;
int& a = b;
std::cout << a + b;

I can get that int is an object type. But;

  1. What is a, b? Are they variables, identifiers, objects? From the definitions, they don't seem like objects. Do they have object types? If so, they should be an object. Do objects have values?

  2. What is the type of a, if these things indeed have types? To this day I thought its type is reference to an int but are references also object types? If so, intand int & seems like completely same object types.

  3. For an rvalue, we know it has a related object in memory so they have related value types, value and data as well. But do rvalues have object types as well? I am asking these because they are region of storage in the sense of being an object wrt. C++ standard but they are not addressable so it seems like they fail to be objects in the Stepanovian sense.

You see, I have a lot of confusion to be cleared up.

meguli
  • 1,426
  • 1
  • 18
  • 35
  • 2
    simple answer: Buy one of them.. https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – JeJo May 24 '18 at 15:57
  • @JeJo I did, I have read couple of them and I am also reading one right now. But I am in need of a clarification. I think this is not so uncommon of a phase in this human activity we call studying. – meguli May 24 '18 at 16:04
  • `What is a, b` - different things. From `An object is a collection of bits in memory that contain a value of a given value type.` then `a` is an object. Not that I am necessarily agreeing with Stepanov's classification. –  May 24 '18 at 16:10
  • What is your definition of variable and identifier? – Robert Andrzejuk May 24 '18 at 16:16
  • The book does not give formal definitions of those as it did with above things. My own definition is not so rigorous and corresponds to everyday use of them in daily programming. I would appreciate if someone cleared up my confusion by giving their real explanations from the standard, in a simplified way because standard is hard to understand for me at this stage. – meguli May 24 '18 at 16:23
  • Note: this question is being [discussed in the site's meta section](https://meta.stackoverflow.com/questions/368546/why-put-on-hold-as-unclear-what-youre-asking). – Hovercraft Full Of Eels May 24 '18 at 21:21

1 Answers1

2
  1. a and b are identifiers, although people will often call them variables when the identifiers refer to variables.

  2. The type of (the variable identified by the identifier) a is int &, also known as a reference to an int. The trick in C and C++ is that you look at the declaration, look at the thing you want to know the type of, remove that from the whole declaration, and what you are left with is the type. So the type of &a (that which a refers to) is int.

    References are also types. But a "reference to an int" is not the same type as an "int". You could see a reference as a pointer type that is automatically dereferenced for you, so int &b = a; std::cout << b; is more or less equal to int *b = &a; std::cout << *b;. In both cases, the variable b is actually holding a pointer value equal to the address of the variable a.

  3. An rvalue does not have to have a related object in memory. For example, in int b = 5, 5 is an rvalue. However, this 5 is not necessarily in memory. The only reason why there is a 5 in memory after that declaration is because the variable b is initialized with the rvalue 5. However, if i would do int b = 1.234, the value 1.234 would not be in memory, only the value 1. But rvalues do have a type. For example, 1.234 is a double.

Community
  • 1
  • 1
G. Sliepen
  • 7,637
  • 1
  • 15
  • 31