[Issue 21065] the new operator should lower to a template function call in object.d

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jul 23 01:46:33 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=21065

Mathias LANG <pro.mathias.lang at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pro.mathias.lang at gmail.com

--- Comment #1 from Mathias LANG <pro.mathias.lang at gmail.com> ---
Regarding your proposed implementation:

> T __makeClassObject(T, A...)(auto ref A);

This would have the effect of producing a potentially large amount of template
instances, as the `auto ref` variadic will match the parameters *exactly* -
something we don't really want, e.g. when instantiating `class Foo { this (int)
{} }` and passing `short`.

Additionally, with this change the implicit conversions don't work anymore
(e.g. passing `ulong.init` will give it a `ulong` which will error when
attempting to implicitly convert it to `int`).

What I found to be working very well when doing this kind of forwarding was to
instead *generate* an overload set and let the compiler do the heavy lifting
for me. In this case, the definition would become:

> template __makeClassObject (T)

And have multiple `__makeClassObject (args)` functions defined in it, each one
accepting exactly the parameter of one constructor definition.

Alternatively, just leave the initialization where it currently is (the
compiler) and just templatize `newclass` to sidestep this issue.

--


More information about the Digitalmars-d-bugs mailing list