alias this and struct allocation

Adam D. Ruppe destructionator at gmail.com
Mon May 6 15:33:47 UTC 2019


On Monday, 6 May 2019 at 14:48:56 UTC, faissaloo wrote:
> misunderstood how allocation works when instantiating a struct 
> that uses alias this:

alias this has no effect on allocation at all. All it does is if

x.y

doesn't compile, it rewrites it to

x.alias_this.y

(or if f(x) doesn't work, it tries f(x.alias_this) too, same idea 
though.)

That's all it does; it is a way to simplify access to or through 
a member.

So the allocation doesn't factor into it.

> Or is base definitely part of the allocation of Child on the 
> heap?

It is definitely part of the Child allocation. But if you are 
passing it to a function, keep in mind it still works as a struct.

void foo(Base b) {}

auto child = new Child();

foo(child);


That gets rewritten into

foo(child.base);

which is passed by value there, unlike classes where it is all 
references. So that might be a source of confusion for you.


More information about the Digitalmars-d-learn mailing list