Perfect forwarding

Jean-Louis Leroy jl at leroy.nyc
Wed Jul 29 12:33:46 UTC 2020


On Wednesday, 29 July 2020 at 10:38:29 UTC, Manu wrote:
> On Wed, Jul 29, 2020 at 9:40 AM Jean-Louis Leroy via 
> Digitalmars-d < digitalmars-d at puremagic.com> wrote:
>
>> On Tuesday, 28 July 2020 at 22:48:16 UTC, Manu wrote:
>> > It is my opinion that if the solution involves a text-mixin, 
>> > the author gets an instant FAIL.
>>
>> But:
>>
>> > 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.
>>
>> Guaranteed failure then? ;-)
>>
>
> Yes.

This is a problem I spent a lot of time on while trying to 
support all the variations of functions in openmethods (and the 
problem is even harder because I need to alter the storage 
classes of a subset of the parameters).

Doesn't this cut it?

module challenge;

template forward(alias fun)
{
     import std.format;
     import std.traits;
     enum name = __traits(identifier, fun);
     static foreach (ovl; __traits(getOverloads, __traits(parent, 
fun), name)) {
         mixin(q{
                 auto ref %s(Parameters!ovl args) {
                     return __traits(parent, fun).%s(args);
                 }
             }.format(name, name));
     }
}

void myfun(int, ref double, out string);

int myfun(in string, inout double);

pragma(msg, typeof(__traits(getOverloads, forward!myfun, 
"myfun")[0]));
pragma(msg, typeof(__traits(getOverloads, forward!myfun, 
"myfun")[1]));

Output:
void(int, ref double, out string)
int(const(string), inout(double))

This deals with storage classes all right.

As for the string mixin, it would be nice to be able to do with 
it, or at least to narrow it to the function name, but in this 
respect D is more C than Lisp.



More information about the Digitalmars-d mailing list