Question about generation of template functions
Jesse Phillips
Jesse.K.Phillips+D at gmail.com
Thu Aug 29 13:26:33 UTC 2019
On Wednesday, 28 August 2019 at 20:56:25 UTC, Machine Code wrote:
> I was writing a recursive function that uses template, I
> thought it would generate the proper template function on the
> fly to match the type in the parameter but it seems to not so
> so and try to use the called function, resulting in the error:
>
>> Error: function foo.serialize!(B).serialize(ref B output) is
>> not callable using argument types (A)
>> cannot pass argument output of type A to parameter ref
>> B output
>> Error: template instance `foo.serialize!(A)` error
>> instantiating
>
> Code:
> void serialize(T)(ref T output)
> {
> import std.traits : hasUDA, getUDAs, isAggregateType;
> import std.meta : Alias;
>
> foreach(fieldName; __traits(derivedMembers, T))
> {
> alias field = Alias!(__traits(getMember, T, fieldName));
> static if(isAggregateType!(typeof(field)))
> {
> serialize!(typeof(field))(output);
> }
> static if(hasUDA!(field, Attr))
> {
> enum className = getUDAs!(field, Attr)[0];
> writefln("className = [%s]", className);
> }
> }
> }
You're asking it to be the field type but passing it output.
serialize!(typeof(field))(output);
More information about the Digitalmars-d-learn
mailing list