www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get pointer or reference of an element in Array(struct)

reply Arun Chandrasekaran <aruncxy gmail.com> writes:
Is there a way to get the pointer or reference of an element in 
Array(T)?

https://run.dlang.io/gist/70fd499afe8438d4877f57aec90c3091?compiler=dmd

The assertion seems to fail below. Value copy is not is intended 
here.

module test;

void main()
{
     struct Data
     {
         int id;
     }

     import std.container.array;
     Array!Data gallery;

     Data d1;
     gallery.insertBack(d1);

     auto d2 = gallery[0];
     d2.id = 1;
     assert(d2.id == gallery[0].id, "neither ref nor pointer");
}
Dec 08 2017
parent reply anonymous <anonymous example.com> writes:
On Saturday, 9 December 2017 at 06:15:16 UTC, Arun Chandrasekaran 
wrote:
 Is there a way to get the pointer or reference of an element in 
 Array(T)?
[...]
     auto d2 = gallery[0];
auto d2 = &gallery[0];
Dec 08 2017
parent reply Arun Chandrasekaran <aruncxy gmail.com> writes:
On Saturday, 9 December 2017 at 06:38:46 UTC, anonymous wrote:
 On Saturday, 9 December 2017 at 06:15:16 UTC, Arun 
 Chandrasekaran wrote:
 Is there a way to get the pointer or reference of an element 
 in Array(T)?
[...]
     auto d2 = gallery[0];
auto d2 = &gallery[0];
Thanks. Just curious why reference can't be obtained here. Saves nasty null checks in most places.
Dec 08 2017
next sibling parent reply David Nadlinger <code klickverbot.at> writes:
On Saturday, 9 December 2017 at 06:46:27 UTC, Arun Chandrasekaran 
wrote:
 Thanks. Just curious why reference can't be obtained here. 
 Saves nasty null checks in most places.
D simply doesn't have a (C++-style) concept of references as part of the type. Arguments can be passed by reference - hence the `ref` keyword -, but "free" references don't exist in the language. The ref in foreach loop variables can be conceptually thought of as a parameter to the loop body as well. (For opApply-based iteration, the loop body indeed gets turned into a function; for "plain" iteration the compiler AST internally has special ref variables, but they are not visible to the language.) -David
Dec 09 2017
parent Arun Chandrasekaran <aruncxy gmail.com> writes:
On Saturday, 9 December 2017 at 19:26:26 UTC, David Nadlinger 
wrote:
 but "free" references don't exist in the language.
To the point! Thanks!
Dec 11 2017
prev sibling parent Azi Hassan <azi.hassan live.fr> writes:
On Saturday, 9 December 2017 at 06:46:27 UTC, Arun Chandrasekaran 
wrote:
 On Saturday, 9 December 2017 at 06:38:46 UTC, anonymous wrote:
 On Saturday, 9 December 2017 at 06:15:16 UTC, Arun 
 Chandrasekaran wrote:
 Is there a way to get the pointer or reference of an element 
 in Array(T)?
[...]
     auto d2 = gallery[0];
auto d2 = &gallery[0];
Thanks. Just curious why reference can't be obtained here. Saves nasty null checks in most places.
In D, structs are passed by value unless otherwise specified. Using a class will yield the expected result.
Dec 09 2017