Anyway to achieve the following
JG
JG at somewhere.com
Fri Aug 13 19:06:17 UTC 2021
On Friday, 13 August 2021 at 17:19:43 UTC, H. S. Teoh wrote:
> On Fri, Aug 13, 2021 at 05:11:50PM +0000, 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 hood. The
> difference is that at the language level they are treated as
> aliases to the original variable, and are therefore guaranteed
> to be non-null.
>
> Note that in D `ref` is not a type constructor but a storage
> qualifier, so you cannot declare a reference variable, you can
> only get one if you pass a variable to a function that takes it
> by ref.
>
>
> T
Thanks for all the replies.
I had a look at emplace but it does not seem to do exactly what I
have in mind.
What I had in mind would have the following behaviour. Suppose we
optionally
allow "in <exp> before the semi-colon at the end of a
declaration. With the following semantics
T x;
T y in &x;
assert(x==y);
assert(&x==&y);
Note that I am not suggesting that the syntax I wrote is what
exists or should exist. I think what I am suggesting is not the
same as say implicitly dereferenced pointers. If you think of the
underlying machine, variables are aliases for locations in memory
where values are stored, and what I am asking is whether it is
possible to alias an arbitrary location (provided it contains the
correct type.) (I guess what I am saying is only conceptually
true variables might end up in registers, but from the point of
view of the language it is true since if v is a variable, then &v
is defined to be its address.)
This would allow things like:
Given:
struct S
{
int x;
int y;
}
You can write:
S s = S(1,2) in new S;
ending up with s being defined on the heap.
Anyway I hope it is clearer what I mean. Is it possible to do
this in d?
More information about the Digitalmars-d-learn
mailing list