Perfect forwarding

Manu turkeyman at gmail.com
Tue Jul 28 22:48:16 UTC 2020


On Sun, Jul 26, 2020 at 11:30 AM Andrei Alexandrescu via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:

> This topic came about during beerconf (it was fun!): Write an idiomatic
> template `forward` that takes an alias `fun` and defines (generates) one
> overload for each overload of `fun`. Example:
>
> template forward(alias fun)
> {
>      ...
> }
>
> Now for this function:
>
> void myfun(int, ref double, out string);
> int myfun(in string, inout double);
>
> the instantiation would be (stylized):
>
> template forward!myfun
> {
>      void myfun(int a, ref double b, out string c)
>      {
>          return myfun(a, b, c);
>      }
>      int myfun(in string a, inout double b)
>      {
>          return myfun(a, b);
>      }
> }
>
> So the challenge is implementing forward() to do this.
>

As someone who's been doing exactly this repeatedly basically-forever, I
can say the exercise is absolutely no fun at all.
It is my opinion that if the solution involves a text-mixin, the author
gets an instant FAIL.
The major hangup in this exercise is dealing with 'storage class', which is
impossible because it's not part of the language, and instantly forces
synthesising strings.
Forwarding the default args is tricky; and I've never managed to produce a
uniform solution that doesn't suffer some edge cases, but I've always made
it work for the specific set of things I'm wrapping.

Template arg forwarding is another level above, but I think that should be
taken as a secondary matter. Solve for normal functions first, and then
maybe there's a hope.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20200729/24b75e4e/attachment.htm>


More information about the Digitalmars-d mailing list