Skip to main content
added 480 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37

Note. — What I call here contents, other authors call value. I'm not calling it value, because I'm using Lakos' definition of value. However, I would agree that the contents of a variable is a value. A physical value. While the value that Lakos' talks about is a platonic logic value.

Note. — A variable contents could be a handle to a resource. It could be an external resources, such as a a handle to a window object or a key to a row in a database table. It could also be an internal resource such as an index to an array (See Entity-Component-System). These are not references as defined above, however they can be considered as such (we could say a pointer is a physical reference, while a handle is a logical reference). If the referenced resources are not copied, they may provide a means for instances to affect each other. See "Rule of Three" below. You may also be interested in RAII. My personal opinion we should not try to archive value semantics with that include handles to external resources, and if we were to, it would require to copy those resources too.

Note. — A variable contents could be a handle to a resource. It could be an external resources, such as a a handle to a window object or a key to a row in a database table. It could also be an internal resource such as an index to an array. These are not references as defined above, however they can be considered as such. If the referenced resources are not copied, they may provide a means for instances to affect each other. See "Rule of Three" below. You may also be interested in RAII. My personal opinion we should not try to archive value semantics with that include handles to external resources, and if we were to, it would require to copy those resources too.

Note. — What I call here contents, other authors call value. I'm not calling it value, because I'm using Lakos' definition of value. However, I would agree that the contents of a variable is a value. A physical value. While the value that Lakos' talks about is a platonic logic value.

Note. — A variable contents could be a handle to a resource. It could be an external resources, such as a a handle to a window object or a key to a row in a database table. It could also be an internal resource such as an index to an array (See Entity-Component-System). These are not references as defined above, however they can be considered as such (we could say a pointer is a physical reference, while a handle is a logical reference). If the referenced resources are not copied, they may provide a means for instances to affect each other. See "Rule of Three" below. You may also be interested in RAII. My personal opinion we should not try to archive value semantics with that include handles to external resources, and if we were to, it would require to copy those resources too.

added 197 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37

Note. — The meaning of "value semantics" in the context of programming has drifted, as evidenced by the definitions provided in the appendix. What follows is my attempt to make sense of it all.

Note. — The meaning of "value semantics" in the context of programming has drifted, as evidenced by the definitions provided in the appendix. What follows is my attempt to make sense of it all.

added 4060 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37

Appendix: Definitions of "Value Semantics", a time line


1998

This template version of List includes a generic iterator and value semantics to store generic data. Value semantics means that List stores instantiated objects, not pointers to objects. During insertion operations, List stores copies of data values instead of storing pointers. Although containers with value semantics allow applications to manage small objects and build-in types easily, many applications cannot tolerate the overhead of copying objects.

– Paul Anderson, Gail Anderson – Navigating C++ and Object-oriented Design


2004

STL containers are value semantic. When a task object is added to an STL container, the task object's allocator and copy constructor are called to clone the original. Similarly, when a task object is removed from an STL container, the task object's deallocator is called to delete the copy. The value semantics may be a performance concern, especially if producers and consumers frequently add tasks to and remove tasks from a queue.

– Ted Yuan – A C++ Producer-Consumer Concurrency Template Library


2004

ValueSemantics for objects-by-value are preserved by copying values between objects. ValueSemantics for objects-by-reference are preserved by using a CopyOnWrite mechanism. I had always thought that the story ended there. Are ValueObjects simply objects that preserve ValueSemantics or is there something more to them?

– PhilGoodwin – Value Objects Can Be Mutable


2014

Types that provide shallow copy (like pointers and references) are said to have pointer semantics or reference semantics (they copy addresses). Types that provide deep copy (like string and vector) are said to have value semantics (they copy the values pointed to). From a user perspective, types with value semantics behave as if no pointers were involved – just values that can be copied. One way of thinking of types with values semantics is that they “work just like integers” as far as copying is concerned.

– Bjarne Stroustrup – Programming: Principles and Practice Using C++


2015

it's (…) possible for a type to be value semantic provided that it keeps one very important property true which is if two objects of the given type have the same value today and we apply in the same salient operation (by salient I mean an operation that is intended to approximate the Platonic type that lives outside of the process that we're using as our model) then after that operation is applied to both objects they will again have the same value or they never did and that is a key property of value semantics.

Another way to say this would be if two objects have the same value then there does not exist a distinguishing sequence of salient operations that will cause them to no longer have the same value.

– John Lakos – An interview with John Lakos


2016

Value semantics amounts to being a guarantee of the independence of the value of variables.

And independence doesn’t mean structural things. What we’re talking about is can one thing affect another. So a type has value semantics if the only way to modify a variable’s value, a variable that has the value semantic type, is through the variable itself. If the only way to modify a variable’s values is through the variable itself, it’s a variable with semantic type.

(…)

The type is value semantic if it’s immune from side effects produced by other things. Not if it’s guaranteed not to perpetrate side effects on other things.

– Alexis Gallagher – Value SEMANTICS (not value types!)


Appendix: Definitions of "Value Semantics", a time line


1998

This template version of List includes a generic iterator and value semantics to store generic data. Value semantics means that List stores instantiated objects, not pointers to objects. During insertion operations, List stores copies of data values instead of storing pointers. Although containers with value semantics allow applications to manage small objects and build-in types easily, many applications cannot tolerate the overhead of copying objects.

– Paul Anderson, Gail Anderson – Navigating C++ and Object-oriented Design


2004

STL containers are value semantic. When a task object is added to an STL container, the task object's allocator and copy constructor are called to clone the original. Similarly, when a task object is removed from an STL container, the task object's deallocator is called to delete the copy. The value semantics may be a performance concern, especially if producers and consumers frequently add tasks to and remove tasks from a queue.

– Ted Yuan – A C++ Producer-Consumer Concurrency Template Library


2004

ValueSemantics for objects-by-value are preserved by copying values between objects. ValueSemantics for objects-by-reference are preserved by using a CopyOnWrite mechanism. I had always thought that the story ended there. Are ValueObjects simply objects that preserve ValueSemantics or is there something more to them?

– PhilGoodwin – Value Objects Can Be Mutable


2014

Types that provide shallow copy (like pointers and references) are said to have pointer semantics or reference semantics (they copy addresses). Types that provide deep copy (like string and vector) are said to have value semantics (they copy the values pointed to). From a user perspective, types with value semantics behave as if no pointers were involved – just values that can be copied. One way of thinking of types with values semantics is that they “work just like integers” as far as copying is concerned.

– Bjarne Stroustrup – Programming: Principles and Practice Using C++


2015

it's (…) possible for a type to be value semantic provided that it keeps one very important property true which is if two objects of the given type have the same value today and we apply in the same salient operation (by salient I mean an operation that is intended to approximate the Platonic type that lives outside of the process that we're using as our model) then after that operation is applied to both objects they will again have the same value or they never did and that is a key property of value semantics.

Another way to say this would be if two objects have the same value then there does not exist a distinguishing sequence of salient operations that will cause them to no longer have the same value.

– John Lakos – An interview with John Lakos


2016

Value semantics amounts to being a guarantee of the independence of the value of variables.

And independence doesn’t mean structural things. What we’re talking about is can one thing affect another. So a type has value semantics if the only way to modify a variable’s value, a variable that has the value semantic type, is through the variable itself. If the only way to modify a variable’s values is through the variable itself, it’s a variable with semantic type.

(…)

The type is value semantic if it’s immune from side effects produced by other things. Not if it’s guaranteed not to perpetrate side effects on other things.

– Alexis Gallagher – Value SEMANTICS (not value types!)

added 65 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37
Loading
added 615 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37
Loading
added 125 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37
Loading
fixed an incorrect indentation that I introduced previously
Source Link
Loading
corrected spellings and improved formatting and punctuation - given that title hierarchy is harder to see with the new formatting... I'm getting rid of it
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37
Loading
corrected spellings and improved formatting and punctuation
Source Link
Loading
added 551 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37
Loading
added 9488 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37
Loading
added 9488 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37
Loading
added 769 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37
Loading
added 710 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37
Loading
added 53 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37
Loading
deleted 143 characters in body
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37
Loading
Source Link
Theraot
  • 9.3k
  • 2
  • 30
  • 37
Loading