digitalmars.D.learn - Get pointer or reference of an element in Array(struct)
- Arun Chandrasekaran (20/20) Dec 08 2017 Is there a way to get the pointer or reference of an element in
- anonymous (4/7) Dec 08 2017 [...]
- Arun Chandrasekaran (3/10) Dec 08 2017 Thanks. Just curious why reference can't be obtained here. Saves
- David Nadlinger (12/14) Dec 09 2017 D simply doesn't have a (C++-style) concept of references as part
- Arun Chandrasekaran (3/4) Dec 11 2017 To the point! Thanks!
- Azi Hassan (4/15) Dec 09 2017 In D, structs are passed by value unless otherwise specified.
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
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
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:Thanks. Just curious why reference can't be obtained here. Saves nasty null checks in most places.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
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
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
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:In D, structs are passed by value unless otherwise specified. Using a class will yield the expected result.On Saturday, 9 December 2017 at 06:15:16 UTC, Arun Chandrasekaran wrote:Thanks. Just curious why reference can't be obtained here. Saves nasty null checks in most places.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 09 2017