Skip to main content
deleted 12 characters in body
Source Link
Géry Ogam
  • 612
  • 3
  • 16

After reading and discussing @Christophe’s and @Theraot’s excellent answers, and drawing inspiration from Bjarne Stroustrup’s and Phil Goodwin’s definitions, I finally came to the following set of definitions, that are close but slightly different (more general) from the previous authors’:

  • Value/reference semantics. — An independency/dependency relation between objects.
  • Value/reference type. — A type that provides copies in value/reference semantics relation.
  • Value/reference object. — An instance of a value/reference type.

Sufficient conditions for value semantics:

  • the objects are deep copies of one another, or
  • the objects do not hold references and are shallow copies of one another, or
  • the objects do not hold references to mutable objects, hold references to immutable objects and are shallow copies of one another, or
  • the objects hold references to mutable objects, hold references to immutable objects and are mutable-deep and immutable-shallow copies of one another.

Sufficient condition for reference semantics:

  • the objects hold references to mutable objects and are shallow copies of one another.

Feel free to give your feedback in comments.

Playground

Memory layout resulting from assignment in C++:

int i{3};              // i:3
int j{i};              // i:3 j:3 (copy of i: j)

int* p{&i};            // i:3 p:&i (alias of i: *p)
int* q{p};             // i:3 p:&i q:&i (copy of p: q, alias of i: *q)
int* r{new int{*p}};   // i:3 p:&i *r:3 r:_ (copy of i: *r)

int** s{&p};           // i:3 p:&i s:&p (alias of p: *s)
int** t{s};            // i:3 p:&i s:&p t:&p (copy of s: t, alias of p: *t)
int** u{new int*{*s}}; // i:3 p:&i s:&p *u:&p u:_ (copy of s: *u, alias of p: **u)

Here i and j are in value semantics relation, p an q are in reference semantics relation, p and r are in value semantics relation, s and t are in reference semantics relation, and s and u are in reference semantics relation.

After reading and discussing @Christophe’s and @Theraot’s excellent answers, and drawing inspiration from Bjarne Stroustrup’s and Phil Goodwin’s definitions, I finally came to the following set of definitions, that are close but slightly different (more general) from the previous authors’:

  • Value/reference semantics. — An independency/dependency relation between objects.
  • Value/reference type. — A type that provides copies in value/reference semantics relation.
  • Value/reference object. — An instance of a value/reference type.

Sufficient conditions for value semantics:

  • the objects are deep copies of one another, or
  • the objects do not hold references and are shallow copies of one another, or
  • the objects do not hold references to mutable objects, hold references to immutable objects and are shallow copies of one another, or
  • the objects hold references to mutable objects, hold references to immutable objects and are mutable-deep and immutable-shallow copies of one another.

Sufficient condition for reference semantics:

  • the objects hold references to mutable objects and are shallow copies of one another.

Feel free to give your feedback in comments.

Playground

Memory layout resulting from assignment in C++:

int i{3};              // i:3
int j{i};              // i:3 j:3 (copy of i: j)

int* p{&i};            // i:3 p:&i (alias of i: *p)
int* q{p};             // i:3 p:&i q:&i (copy of p: q, alias of i: *q)
int* r{new int{*p}};   // i:3 p:&i *r:3 r:_ (copy of i: *r)

int** s{&p};           // i:3 p:&i s:&p (alias of p: *s)
int** t{s};            // i:3 p:&i s:&p t:&p (copy of s: t, alias of p: *t)
int** u{new int*{*s}}; // i:3 p:&i s:&p *u:&p u:_ (copy of s: *u, alias of p: **u)

Here i and j are in value semantics relation, p an q are in reference semantics relation, p and r are in value semantics relation, s and t are in reference semantics relation, and s and u are in reference semantics relation.

After discussing @Christophe’s and @Theraot’s excellent answers, and drawing inspiration from Bjarne Stroustrup’s and Phil Goodwin’s definitions, I finally came to the following set of definitions, that are close but slightly different (more general) from the previous authors’:

  • Value/reference semantics. — An independency/dependency relation between objects.
  • Value/reference type. — A type that provides copies in value/reference semantics relation.
  • Value/reference object. — An instance of a value/reference type.

Sufficient conditions for value semantics:

  • the objects are deep copies of one another, or
  • the objects do not hold references and are shallow copies of one another, or
  • the objects do not hold references to mutable objects, hold references to immutable objects and are shallow copies of one another, or
  • the objects hold references to mutable objects, hold references to immutable objects and are mutable-deep and immutable-shallow copies of one another.

Sufficient condition for reference semantics:

  • the objects hold references to mutable objects and are shallow copies of one another.

Feel free to give your feedback in comments.

Playground

Memory layout resulting from assignment in C++:

int i{3};              // i:3
int j{i};              // i:3 j:3 (copy of i: j)

int* p{&i};            // i:3 p:&i (alias of i: *p)
int* q{p};             // i:3 p:&i q:&i (copy of p: q, alias of i: *q)
int* r{new int{*p}};   // i:3 p:&i *r:3 r:_ (copy of i: *r)

int** s{&p};           // i:3 p:&i s:&p (alias of p: *s)
int** t{s};            // i:3 p:&i s:&p t:&p (copy of s: t, alias of p: *t)
int** u{new int*{*s}}; // i:3 p:&i s:&p *u:&p u:_ (copy of s: *u, alias of p: **u)

Here i and j are in value semantics relation, p an q are in reference semantics relation, p and r are in value semantics relation, s and t are in reference semantics relation, and s and u are in reference semantics relation.

added 248 characters in body
Source Link
Géry Ogam
  • 612
  • 3
  • 16

After reading and discussing @Christophe’s and @Theraot’s excellent answers, and drawing inspiration from Bjarne Stroustrup’s and Phil Goodwin’s definitions, I finally came to the following set of definitions, that are close but slightly different (more general) from the previous authors’:

  • Value/reference semantics. — An independency/dependency relation between objects.
  • Value/reference type. — A type that provides copies in value/reference semantics relation.
  • Value/reference object. — An instance of a value/reference type.

Sufficient conditions for value semantics:

  • the objects are deep copies of one another, or
  • the objects do not hold references and are shallow copies of one another, or
  • the objects do not hold references to mutable objects, hold references to immutable objects and are shallow copies of one another, or
  • the objects hold references to mutable objects, hold references to immutable objects and are mutable-deep and immutable-shallow copies of one another.

Sufficient condition for reference semantics:

  • the objects hold references to mutable objects and are shallow copies of one another.

Feel free to give your feedback in comments.

Playground

Memory layout resulting from assignment in C++:

int i{3};              // i:3
int j{i};              // i:3 j:3 (copy of i: j)

int* p{&i};            // i:3 p:&i (alias of i: *p)
int* q{p};             // i:3 p:&i q:&i (copy of p: q, alias of i: *q)
int* r{new int{*p}};   // i:3 p:&i *r:3 r:_ (copy of i: *r)

int** s{&p};           // i:3 p:&i s:&p (alias of p: *s)
int** t{s};            // i:3 p:&i s:&p t:&p (copy of s: t, alias of p: *t)
int** u{new int*{*s}}; // i:3 p:&i s:&p *u:&p u:_ (copy of s: *u, alias of p: **u)

Here i and j are in value semantics relation, p an q are in reference semantics relation, p and r are in value semantics relation, s and t are in reference semantics relation, and s and u are in reference semantics relation.

After reading and discussing @Christophe’s and @Theraot’s excellent answers, and drawing inspiration from Bjarne Stroustrup’s and Phil Goodwin’s definitions, I finally came to the following set of definitions, that are close but slightly different (more general) from the previous authors’:

  • Value/reference semantics. — An independency/dependency relation between objects.
  • Value/reference type. — A type that provides copies in value/reference semantics relation.
  • Value/reference object. — An instance of a value/reference type.

Sufficient conditions for value semantics:

  • the objects are deep copies of one another, or
  • the objects do not hold references and are shallow copies of one another, or
  • the objects do not hold references to mutable objects, hold references to immutable objects and are shallow copies of one another, or
  • the objects hold references to mutable objects, hold references to immutable objects and are mutable-deep and immutable-shallow copies of one another.

Sufficient condition for reference semantics:

  • the objects hold references to mutable objects and are shallow copies of one another.

Feel free to give your feedback in comments.

Playground

Memory layout resulting from assignment in C++:

int i{3};              // i:3
int j{i};              // i:3 j:3 (copy of i: j)

int* p{&i};            // i:3 p:&i (alias of i: *p)
int* q{p};             // i:3 p:&i q:&i (copy of p: q, alias of i: *q)
int* r{new int{*p}};   // i:3 p:&i *r:3 r:_ (copy of i: *r)

int** s{&p};           // i:3 p:&i s:&p (alias of p: *s)
int** t{s};            // i:3 p:&i s:&p t:&p (copy of s: t, alias of p: *t)
int** u{new int*{*s}}; // i:3 p:&i s:&p *u:&p u:_ (copy of s: *u, alias of p: **u)

After reading and discussing @Christophe’s and @Theraot’s excellent answers, and drawing inspiration from Bjarne Stroustrup’s and Phil Goodwin’s definitions, I finally came to the following set of definitions, that are close but slightly different (more general) from the previous authors’:

  • Value/reference semantics. — An independency/dependency relation between objects.
  • Value/reference type. — A type that provides copies in value/reference semantics relation.
  • Value/reference object. — An instance of a value/reference type.

Sufficient conditions for value semantics:

  • the objects are deep copies of one another, or
  • the objects do not hold references and are shallow copies of one another, or
  • the objects do not hold references to mutable objects, hold references to immutable objects and are shallow copies of one another, or
  • the objects hold references to mutable objects, hold references to immutable objects and are mutable-deep and immutable-shallow copies of one another.

Sufficient condition for reference semantics:

  • the objects hold references to mutable objects and are shallow copies of one another.

Feel free to give your feedback in comments.

Playground

Memory layout resulting from assignment in C++:

int i{3};              // i:3
int j{i};              // i:3 j:3 (copy of i: j)

int* p{&i};            // i:3 p:&i (alias of i: *p)
int* q{p};             // i:3 p:&i q:&i (copy of p: q, alias of i: *q)
int* r{new int{*p}};   // i:3 p:&i *r:3 r:_ (copy of i: *r)

int** s{&p};           // i:3 p:&i s:&p (alias of p: *s)
int** t{s};            // i:3 p:&i s:&p t:&p (copy of s: t, alias of p: *t)
int** u{new int*{*s}}; // i:3 p:&i s:&p *u:&p u:_ (copy of s: *u, alias of p: **u)

Here i and j are in value semantics relation, p an q are in reference semantics relation, p and r are in value semantics relation, s and t are in reference semantics relation, and s and u are in reference semantics relation.

added 379 characters in body
Source Link
Géry Ogam
  • 612
  • 3
  • 16

After reading and discussing @Christophe’s and @Theraot’s excellent answers, and drawing inspiration from Bjarne Stroustrup’s and Phil Goodwin’s definitions, I finally came to the following set of definitions, that are close but slightly different (more general) from the previous authors’:

  • Value/reference semantics. — An independency/dependency relation between objects.
  • Value/reference type. — A type that provides copies in value/reference semantics relation.
  • Value/reference object. — An instance of a value/reference type.

Sufficient conditions for value semantics:

  • the objects are deep copies of one another, or
  • the objects do not hold references and are shallow copies of one another, or
  • the objects do not hold references to mutable objects, hold references to immutable objects and are shallow copies of one another, or
  • the objects hold references to mutable objects, hold references to immutable objects and are mutable-deep and immutable-shallow copies of one another.

Sufficient condition for reference semantics:

  • the objects hold references to mutable objects and are shallow copies of one another.

Feel free to give your feedback in comments.

Playground

Memory layout resulting from assignment in C++:

int i{3};               // i:3
int j{i};               // i:3 j:3 (copy of i: j)

int* p{&i};             // i:3 p:&i (alias of i: *p)
int* q{p};              // i:3 p:&i q:&i (copy of p: q, alias of i: *q)
int* r{new int{*p}};    // i:3 p:&i *r:3 r:_ (copy of i: *r)

int** s{&p};            // i:3 p:&i s:&p (alias of p: *s)
int** t{s};             // i:3 p:&i s:&p t:&p (copy of s: t, alias of p: *t)
int** u{new int*{*s}};  // i:3 p:&i s:&p *u:&p u:_ (copy of s: *u, alias of p: **u)

After reading and discussing @Christophe’s and @Theraot’s excellent answers, and drawing inspiration from Bjarne Stroustrup’s and Phil Goodwin’s definitions, I finally came to the following set of definitions, that are close but slightly different (more general) from the previous authors’:

  • Value/reference semantics. — An independency/dependency relation between objects.
  • Value/reference type. — A type that provides copies in value/reference semantics relation.
  • Value/reference object. — An instance of a value/reference type.

Sufficient conditions for value semantics:

  • the objects are deep copies of one another, or
  • the objects do not hold references and are shallow copies of one another, or
  • the objects do not hold references to mutable objects, hold references to immutable objects and are shallow copies of one another, or
  • the objects hold references to mutable objects, hold references to immutable objects and are mutable-deep and immutable-shallow copies of one another.

Sufficient condition for reference semantics:

  • the objects hold references to mutable objects and are shallow copies of one another.

Feel free to give your feedback in comments.

Playground

int i{3};               // i:3
int j{i};               // i:3 j:3

int* p{&i};             // i:3 p:&i
int* q{p};              // i:3 p:&i q:&i
int* r{new int{*p}};    // i:3 p:&i *r:3 r:_

int** s{&p};            // i:3 p:&i s:&p
int** t{s};             // i:3 p:&i s:&p t:&p
int** u{new int*{*s}};  // i:3 p:&i s:&p *u:&p u:_

After reading and discussing @Christophe’s and @Theraot’s excellent answers, and drawing inspiration from Bjarne Stroustrup’s and Phil Goodwin’s definitions, I finally came to the following set of definitions, that are close but slightly different (more general) from the previous authors’:

  • Value/reference semantics. — An independency/dependency relation between objects.
  • Value/reference type. — A type that provides copies in value/reference semantics relation.
  • Value/reference object. — An instance of a value/reference type.

Sufficient conditions for value semantics:

  • the objects are deep copies of one another, or
  • the objects do not hold references and are shallow copies of one another, or
  • the objects do not hold references to mutable objects, hold references to immutable objects and are shallow copies of one another, or
  • the objects hold references to mutable objects, hold references to immutable objects and are mutable-deep and immutable-shallow copies of one another.

Sufficient condition for reference semantics:

  • the objects hold references to mutable objects and are shallow copies of one another.

Feel free to give your feedback in comments.

Playground

Memory layout resulting from assignment in C++:

int i{3};              // i:3
int j{i};              // i:3 j:3 (copy of i: j)

int* p{&i};            // i:3 p:&i (alias of i: *p)
int* q{p};             // i:3 p:&i q:&i (copy of p: q, alias of i: *q)
int* r{new int{*p}};   // i:3 p:&i *r:3 r:_ (copy of i: *r)

int** s{&p};           // i:3 p:&i s:&p (alias of p: *s)
int** t{s};            // i:3 p:&i s:&p t:&p (copy of s: t, alias of p: *t)
int** u{new int*{*s}}; // i:3 p:&i s:&p *u:&p u:_ (copy of s: *u, alias of p: **u)
added 379 characters in body
Source Link
Géry Ogam
  • 612
  • 3
  • 16
Loading
added 191 characters in body
Source Link
Géry Ogam
  • 612
  • 3
  • 16
Loading
deleted 113 characters in body
Source Link
Géry Ogam
  • 612
  • 3
  • 16
Loading
deleted 4 characters in body
Source Link
Géry Ogam
  • 612
  • 3
  • 16
Loading
Source Link
Géry Ogam
  • 612
  • 3
  • 16
Loading