4

I was reading about value types and values and objects were mentioned as if they meant something different, so I assume they do. I then tried to do some research but without luck. I know that a value is an expression that cannot be evaluated further, but isn't an object just the "container" for a value? Also, based on my terminological understanding of expressions, would it be weird to say that an object holds an expression(since a value is an expression)?

I will not start making too many guesses because that could will probably just complicate it all and might redirect the focus away from the difference between values and objects.

Edit: To the people who have marked this as "too-broad", isn't the following question just as broad if not broader What are rvalues, lvalues, xvalues, glvalues, and prvalues? Also, there are so many other "difference between" questions out there on SO, and many of them are as broad as the one I linked.. So why is this particular question not valid?

  • Objects are user defined variables – ssovukluk Sep 18 '17 at 20:49
  • @ssovukluk I can't remember the c++ standard documentation refers to something like _objects_. I'd be happily disproven. – user0042 Sep 18 '17 at 20:51
  • @ssovukluk What is a user defined variable as opposed to a non-user defined one? – juanchopanza Sep 18 '17 at 20:52
  • 4
    The same difference between a cup and water. – StoryTeller - Unslander Monica Sep 18 '17 at 20:52
  • I would say that objects have values. A value is a concept and the object holds the data representing that concept. – Galik Sep 18 '17 at 20:54
  • 1
    @user0042 Like **1.8 The C++ object model [intro.object]** for example? – juanchopanza Sep 18 '17 at 20:54
  • C++17 tightens up the definition of an object a little to only include *initialized* storage I believe. – Galik Sep 18 '17 at 20:55
  • 3
    Expressions are not values, but you could say that they have – or denote – values. The expression "the present king of Norway" isn't the present king of Norway, even though that's what it means. – molbdnilo Sep 18 '17 at 20:57
  • @Galik so objects are the addresses where the values are stored? And can you then say that objects stores expressions, since values are expressions? :/ –  Sep 18 '17 at 21:18
  • @molbdnilo by why are values defined as expressions if they are not expressions? :/ –  Sep 18 '17 at 21:20
  • I would not say an expression is a value. But it has a value which may be stored in an object (after the expression is resolved) – Galik Sep 18 '17 at 21:21
  • According to the C++11 standard: **"An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects."** – Galik Sep 18 '17 at 21:44
  • 1
    Voted to reopen. "I don't know" is not a reason to close the question; this is a frequent area of confusion (as evinced by the comments so far) – M.M Sep 18 '17 at 21:53
  • 1
    Also, the standard (even the latest C++17 draft) doesn't actually define the term "value" . We're left to make our own guesses about its meaning based on the contexts that the standard uses it. For example , what is the value of the expression `"a"s + "b"s` ? Is it correct to say "the value of the expression *is* a temporary object"? If so, then is it also correct to talk about the value of that temporary object, since all objects have values? And what about `int&& x = 2;` ? – M.M Sep 18 '17 at 22:09
  • 1
    @sdsadasdasd: "*To the people who have marked this as "too-broad", isn't the following question just as broad if not broader What are rvalues, lvalues, xvalues, glvalues, and prvalues?*" No. Those are well-defined by the C++ standard. Object is well-defined, but "value type" is not. It's a conceptual idiom, not a standard-defined construct. – Nicol Bolas Sep 19 '17 at 03:44
  • @StoryTeller why do you think there is no exact definition of "value"? and why does the wiki article state that a value is an expression why everybody here says otherwise? –  Sep 19 '17 at 18:48

2 Answers2

5

A value is a more abstract concept than an object.

Like those mentioned in comments, the difference/relationship between a value and an object is similar to that between water and a cup.

You need a container, which in this case is a cup, to hold the water, or it will leak. Likewise, you need an object to hold values so they don't get lost or overwritten very soon.

An object always has its dedicated storage area (in memory), whereas a value's life span can be as little as a single instruction (movl %eax, $1).

iBug
  • 35,554
  • 7
  • 89
  • 134
  • I believe that I need a bit more detailed explanation to really understand the difference. If objects are defined as regions of storage and values are defined as expressions that cannot be evaluated further(on wikipedia), are you saying that regions of storage hold expressions that cannot be evaluated further? After discovering the definition of values, things got confusing... –  Sep 19 '17 at 14:43
3

This term means slightly different things in different parts of computer science (as you can see from the discussion on its wiki page). I'm framing this answer in the context of C++ and "value types" mentioned in the question - the authoritative references here would be Alexander Stepanov's "Elements of Programming" and the lectures on value-semantic types by John Lakos.

A value is a member of a set that exists independently of its representation.

The member of the set of integers that can be repesented as "fifteen", "15", "0xf", "十五", or "ⅩⅤ" is the same, universal, value. I can write it down on two pieces of paper, add one to both, and both pieces of paper will have a representation of the same exact value 16, even if I use different notations (this is what it means to have "value semantics")

An object is a region of computer memory that consists of bits (binary digits), and has a type. One of the things the type does is determine the mapping between the state of those bits and the value we treat the object as.

An object of type int8_t consisting of the bits 00001111 is, on existing computers, considered to represent the integer value "fifteen".

Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • 1
    Thanks! But it is still not all clear. So this [wiki article](https://en.wikipedia.org/wiki/Value_(computer_science)) defines _values_ as an _expression_ that cannot be evaluated any further; they claim that _values_ are _expressions_. This confuses everything. From what you said and what others have said, you all seem to _not_ define values as expressions, so why do they, or are values really expressions? Also, from my terminological understanding of _expressions_, it seems weird that there are _expressions_ stored in memory. Can you help? –  Sep 20 '17 at 07:17
  • @sdsadasdasd note the wiki page says "dubious-discuss" right there. If you want to learn more, click on the "discuss" – Cubbi Sep 20 '17 at 09:52
  • But it still claims that values are _expressions_? –  Sep 20 '17 at 09:55
  • @sdsadasdasd Click on the "discuss". There is an extensive discussion because there are different definitions of "value" in different branches of computer science. You asked about "value types" and "C++", so that was the context that framed my asnwer. – Cubbi Sep 20 '17 at 10:15
  • @sdsadasdasd added two references to the answer which I believe are authoritative in this context (and whose ideas I was trying to summarize) – Cubbi Sep 20 '17 at 10:24
  • 1
    But still, are you saying that the wiki article's statement about values is _wrong_? So you are saying that _values_ are not _expressions_? –  Sep 20 '17 at 10:33
  • I just do not want to misunderstand anything :) –  Sep 20 '17 at 11:18