general questions regarding value and reference semantics
Ali Çehreli
acehreli at yahoo.com
Sun Apr 28 20:46:41 PDT 2013
On 04/28/2013 05:22 PM, WhatMeWorry wrote:
> Does a fixed-length, static array behave with value semantics
Yes, a static array has value semantics. When assigned, a variable of a
static array type will get its own elements, separate from the right
hand side's elements.
int[2] a;
int[2] b = a; // b and a have separate elements
int[2] c;
b = c; // b's elements are copied from c's
> while a dynamic array behave with reference semantics?
Yes, because a dynamic array does not own any elements. It is a handle
to reach those elements.
> And then what to make of Phobos' std.container, when it says "DList uses
> neither reference nor value semantics." Is there some hybrid or new
> undiscovered semantic I've never heard about?
I don't have experience with DList but looking at its documentation, a
DList is obviously a reference to a part of something called a chain. In
that sense, DList has reference semantics.
On the other hand, every DList variable owns the chain as well, because
they can actually remove elemnents from it regardless of whether others
are still referring parts of it. (Apparently, it is not undefined
behavior nor it invalidates other DList variables: The other variables
simply start seeing a shorter chain of elements.)
Ali
More information about the Digitalmars-d-learn
mailing list