Are stack+heap classes possible in D?

WhatMeWorry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 16 23:02:46 PDT 2015


On Sunday, 14 June 2015 at 01:31:25 UTC, Adam D. Ruppe wrote:
> On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote:
>> I have read that in D structs are always allocated on the 
>> stack while classes are always allocated on the heap.
>
> That's not true; it is a really common misconception.
>
> Putting a struct on the heap is trivial and built into the 
> language: `S* s = new S();`
>
> Putting a class on the stack is a bit trickier, but the 
> standard library provides a helper to do it: 
> http://dlang.org/phobos/std_typecons.html#scoped follow the 
> examples given there.

True, but wasn't there a conscientious decision to make structs 
more amenable to value semantics while classes are more conducive 
to reference semantics.

I guess the question would be why would one want a struct on the 
heap and a class on the stack?  Performance reasons?


More information about the Digitalmars-d-learn mailing list