DMD 0.177 release
Jarrett Billingsley
kb3ctd2 at yahoo.com
Tue Dec 12 07:59:34 PST 2006
"Pragma" <ericanderton at yahoo.removeme.com> wrote in message
news:elmhtj$1qv7$1 at digitaldaemon.com...
> Well, I think it's one of the reasons why we don't have ctors for structs
> right now. The preferred syntax for a "struct ctor" would probably be
> this:
>
> S foo = S(a,b,c);
>
> Which is indistinct from "static opCall". Throwing 'new' in there
> wouldn't work either, since that would be a dynamic allocation:
>
> S* foo = new S(a,b,c);
>
> So that leaves us with "something else" that provides both a way to invoke
> a ctor, yet allocates the data on the stack and doesn't force you to
> create an additional copy:
>
> S foo(a,b,c); // c++ style
> S foo = stackalloc S(a,b,c); // alloca() style (in place of new)
> S foo = new(stack) S(a,b,c): // another idea
Personally for in-place stack allocation, I like the C++ style the best.
It's the simplest to deal with for the compiler (it's 100% obvious that it's
being created on the stack and there's no need for optimization to take care
of anything). And it makes sense -- it says "make this a local variable,
but call the constructor on it as well." There's no assignment so it
doesn't look like any copying is going on.
More information about the Digitalmars-d-announce
mailing list