new should lower to a template function call

Adam D. Ruppe destructionator at gmail.com
Thu Jul 23 13:12:48 UTC 2020


On Thursday, 23 July 2020 at 05:12:30 UTC, Walter Bright wrote:
> The compiler already does this if the variable being new`d is 
> `scope`.

Yeah, I know, I just want to make sure that this doesn't change 
and that the spec is written to allow it.

Right now, the spec says:

"NewExpressions are used to allocate memory on the garbage 
collected heap (default) or using a class or struct specific 
allocator.

[...]

If a NewExpression is used as an initializer for a function local 
variable with scope storage class, and the ArgumentList to new is 
empty, then the instance is allocated on the stack rather than 
the heap or using the class specific allocator. "


So it arguably is a violation of the spec to use stack 
optimization without the scope keyword, and vice versa. It should 
really just specify it allocates and initializes the class while 
leaving where it does it as implementation-defined.


When lowering to a template, it should also be careful to say it 
will not necessarily lower to the same thing; a user should NOT 
expect the template to actually even be called and the template 
is free to change its strategy.


So the implementation right now is ok and I expect the lowering 
to a template will be equally OK. Just be careful not to specify 
too much.


More information about the Digitalmars-d mailing list