How can i increase max number recursive template expansions?

QAston qaston at gmail.com
Sun Jul 7 14:01:03 PDT 2013


On Sunday, 7 July 2013 at 20:28:12 UTC, John Colvin wrote:
> On Sunday, 7 July 2013 at 20:22:59 UTC, Simen Kjaeraas wrote:
>> On 2013-07-07, 21:55, QAston wrote:
>>
>>> I have a large enum in my code (opcodes for a protocol) - 
>>> using std.traits.EnumMembers gives me a recursive template 
>>> error.
>>>
>>> How can i increase max number recursive template expansions?
>>
>> You can't. However, you can amend std.traits.EnumMembers to 
>> work
>> with larger enums by using this version:
>>
>>
>>
>> import std.typetuple;
>>
>> template EnumMembers(E)
>>    if (is(E == enum))
>> {
>>    // Supply the specified identifier to an constant value.
>>    template WithIdentifier(string ident)
>>    {
>>        static if (ident == "Symbolize")
>>        {
>>            template Symbolize(alias value)
>>            {
>>                enum Symbolize = value;
>>            }
>>        }
>>        else
>>        {
>>            mixin("template Symbolize(alias "~ ident ~")"
>>                 ~"{"
>>                     ~"alias "~ ident ~" Symbolize;"
>>                 ~"}");
>>        }
>>    }
>>
>>    template EnumSpecificMembers(names...)
>>    {
>>        static if (names.length > 200)
>>        {
>>            alias TypeTuple!(
>>                    EnumSpecificMembers!(names[0..$/2]),
>>                    EnumSpecificMembers!(names[$/2..$]),
>>                ) EnumSpecificMembers;
>>        }
>>        else static if (names.length > 0)
>>        {
>>            alias TypeTuple!(
>>                    WithIdentifier!(names[0])
>>                        .Symbolize!(__traits(getMember, E, 
>> names[0])),
>>                    EnumSpecificMembers!(names[1 .. $]),
>>                ) EnumSpecificMembers;
>>        }
>>        else
>>        {
>>            alias TypeTuple!() EnumSpecificMembers;
>>        }
>>    }
>>
>>    alias EnumSpecificMembers!(__traits(allMembers, E)) 
>> EnumMembers;
>> }
>>
>>
>>
>> Here, this line:
>>        static if (names.length > 200)
>> uses divide-and-conquer to reduce the number of template 
>> instantiations.
>
> We came up with almost identical solutions, posted within 19 
> seconds of each other!

Thanks guys, you're awesome!


More information about the Digitalmars-d-learn mailing list