Re: Anyway to achieve the following

2021-08-17 Thread Rekel via Digitalmars-d-learn
On Saturday, 14 August 2021 at 20:50:47 UTC, Carl Sturtivant wrote: ``` struct S { int x = 1234; } void main() { import std.stdio; S s; //construction of a using &(s.x) auto a = Ref!(int)(&s.x); writeln(a); //displays 1234 s.x += 1; writeln(a); //displays 1235 a += 1;

Re: Anyway to achieve the following

2021-08-17 Thread Johan via Digitalmars-d-learn
On Monday, 16 August 2021 at 19:30:19 UTC, JG wrote: On Sunday, 15 August 2021 at 21:53:14 UTC, Carl Sturtivant wrote: On Sunday, 15 August 2021 at 07:10:17 UTC, JG wrote: [...] What you are asking for are reference variables. C++ has them: the example here illustrates the behavior you want.

Re: Anyway to achieve the following

2021-08-16 Thread JG via Digitalmars-d-learn
On Sunday, 15 August 2021 at 21:53:14 UTC, Carl Sturtivant wrote: On Sunday, 15 August 2021 at 07:10:17 UTC, JG wrote: [...] What you are asking for are reference variables. C++ has them: the example here illustrates the behavior you want. https://www.geeksforgeeks.org/references-in-c/ [...

Re: Anyway to achieve the following

2021-08-15 Thread Carl Sturtivant via Digitalmars-d-learn
On Sunday, 15 August 2021 at 07:10:17 UTC, JG wrote: Hi, This is exactly the behaviour I was trying to obtain. It however comes with a fair amount of overhead, as can be seen in the following llvm ir: [...] What you are asking for are reference variables. C++ has them: the example here il

Re: Anyway to achieve the following

2021-08-15 Thread Johan via Digitalmars-d-learn
On Sunday, 15 August 2021 at 16:49:22 UTC, Paul Backus wrote: On Sunday, 15 August 2021 at 07:10:17 UTC, JG wrote: Hi, This is exactly the behaviour I was trying to obtain. It however comes with a fair amount of overhead, as can be seen in the following llvm ir: [...] I'm not really fami

Re: Anyway to achieve the following

2021-08-15 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 15 August 2021 at 07:10:17 UTC, JG wrote: Hi, This is exactly the behaviour I was trying to obtain. It however comes with a fair amount of overhead, as can be seen in the following llvm ir: [...] I'm not really familiar with llvm ir, but looking at it on godbolt, it seems like

Re: Anyway to achieve the following

2021-08-15 Thread JG via Digitalmars-d-learn
On Saturday, 14 August 2021 at 20:50:47 UTC, Carl Sturtivant wrote: ``` struct S { int x = 1234; } void main() { import std.stdio; S s; //construction of a using &(s.x) auto a = Ref!(int)(&s.x); writeln(a); //displays 1234 s.x += 1; writeln(a); //displays 1235 a += 1;

Re: Anyway to achieve the following

2021-08-14 Thread Carl Sturtivant via Digitalmars-d-learn
``` struct S { int x = 1234; } void main() { import std.stdio; S s; //construction of a using &(s.x) auto a = Ref!(int)(&s.x); writeln(a); //displays 1234 s.x += 1; writeln(a); //displays 1235 a += 1; writeln(s.x); //displays 1236 } struct Ref(T) { T* ptr; this(T*

Re: Anyway to achieve the following

2021-08-13 Thread pilger via Digitalmars-d-learn
On Friday, 13 August 2021 at 19:06:17 UTC, JG wrote: Anyway I hope it is clearer what I mean. Is it possible to do this in d? union S { int x; int a; } void main() { S s= S(1234); writeln(s.a); //displays 1234 s.x = s.x+1; writeln(s.a); //displays 1235 s.a = s.a +1; writ

Re: Anyway to achieve the following

2021-08-13 Thread JG via Digitalmars-d-learn
On Friday, 13 August 2021 at 17:19:43 UTC, H. S. Teoh wrote: On Fri, Aug 13, 2021 at 05:11:50PM +, Rekel via Digitalmars-d-learn wrote: [...] For anyone more experienced with C, I'm not well known with references but are those semantically similar to the idea of using a type at a predefined

Re: Anyway to achieve the following

2021-08-13 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Aug 13, 2021 at 05:11:50PM +, Rekel via Digitalmars-d-learn wrote: [...] > For anyone more experienced with C, I'm not well known with references > but are those semantically similar to the idea of using a type at a > predefined location? References are essentially pointers under the h

Re: Anyway to achieve the following

2021-08-13 Thread Rekel via Digitalmars-d-learn
On Friday, 13 August 2021 at 09:10:18 UTC, Tejas wrote: On Friday, 13 August 2021 at 08:25:33 UTC, JG wrote: Suppose one has a pointer p of type T*. Can on declare variable a of type T which is stored in the location pointed to by p? Umm is this what you want? ```d import std.stdio; struct S

Re: Anyway to achieve the following

2021-08-13 Thread Paul Backus via Digitalmars-d-learn
On Friday, 13 August 2021 at 09:30:25 UTC, Ali Çehreli wrote: (core.lifetime is not on dlang.org at the moment for me but it is under /usr/include/dmd/druntime/import/core on my computer.) It's also on dpldocs.info: https://dpldocs.info/experimental-docs/core.lifetime.html

Re: Anyway to achieve the following

2021-08-13 Thread Ali Çehreli via Digitalmars-d-learn
On 8/13/21 1:25 AM, JG wrote: > Suppose one has a pointer p of type T*. > Can on declare variable a of type T which is stored in the location > pointed to by p? You may be looking for core.lifetime.emplace. (core.lifetime is not on dlang.org at the moment for me but it is under /usr/include/dm

Re: Anyway to achieve the following

2021-08-13 Thread Tejas via Digitalmars-d-learn
On Friday, 13 August 2021 at 08:25:33 UTC, JG wrote: Suppose one has a pointer p of type T*. Can on declare variable a of type T which is stored in the location pointed to by p? As an example if we have: struct S { int x = 1234; } void main() { S s; //unkn

Anyway to achieve the following

2021-08-13 Thread JG via Digitalmars-d-learn
Suppose one has a pointer p of type T*. Can on declare variable a of type T which is stored in the location pointed to by p? As an example if we have: struct S { int x = 1234; } void main() { S s; //unknown construction of a using &(s.x) writeln(a);