Cannot get this C++ example migrated to D

Skippy Skippy at gmail.com
Sun Apr 16 22:25:11 UTC 2023


On Sunday, 16 April 2023 at 08:38:55 UTC, Ali Çehreli wrote:
> On 4/16/23 00:46, Skippy wrote:
>
> > I wish D had value type classes as well.
>
> That cannot work due to the slicing problem.
>
> C++ cannot have value type classes either for the same reason. 
> The difference there is that the enforcement is by guidelines 
> (e.g. "never pass class objects by value to functions", not by 
> language).
>
> If you want class objects on the stack, you have two options:
>
> - The scope keyword
>
> - The scoped template from Phobos
>
> Ali

IMO, there is no need to 'force' reference semantics on the 
example provided (i.e the slicing problem does not apply here).

I was under the impression scope (the keyword) was being 
deprecated?

I could have used a struct of course, but my mental model of a 
struct is a C-like struct, not a C++ like struct. I like that 
mental model - which is the reason why I like C++'s value type 
classes.

Then there is the issue of the D compiler assigning a 'default 
value' to an object variable that hasn't yet been assigned a 
value: Object o;

But.. that's a separate discussion.


More information about the Digitalmars-d-learn mailing list