Sequence separation
Lodovico Giaretta via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Aug 17 12:21:57 PDT 2016
On Wednesday, 17 August 2016 at 19:15:48 UTC, ag0aep6g wrote:
> On 08/17/2016 08:38 PM, Engine Machine wrote:
>> On Wednesday, 17 August 2016 at 08:37:32 UTC, Lodovico
>> Giaretta wrote:
> [...]
>>> You mean something like:
>>>
>>> struct MySequence(Args...)
>>> {
>>> enum length = Args.length;
>>> alias args = Args;
>>> }
>>>
>>> alias x = MySequence!(a, b, MySequence!(c, d));
>>>
>>> static assert(x.length == 3)
>>> static assert(x.args[2].length == 2);
>>
>> Thanks, basically works.
>>
>> How can I test, though, if a argument uses a MySequence? I
>> can't do if
>> (Args[0] == MySequence) because MySequence is templated. While
>> I could
>> test for a length, that doesn't work because some types have a
>> length. I
>> could add another enum to MySequence, but again, not safe.
>>
>> I could do some string tests, but that doesn't work.
>>
>> in your exmaple,
>>
>> if (x.args[2] == MySequence) ??
>>
>> I simply need to differentiate between a parameter/arg being a
>> MySequence and not.
>
> With MySequence being a type, you can do this:
>
> ----
> static if (is(x.args[2] == MySequence!Args, Args ...))
> {
> ...
> }
> ----
>
> Aside from this check, there is probably not much use for
> MySequence being a type. So I'm be tempted to find a way to do
> the check with a raw template MySequence.
import std.traits: TemplateOf;
static if (__traits(isSame, TemplateOf!(x.args[2]), MySequence))
{
...
}
std.traits.TemplateOf extracts the symbol representing the
uninstantiated template.
__traits(isSame, symbol1, symbol2) evaluates at compile time to
true if and only if the two symbols represent the same thing (be
it a type, an uninstantiated template, an instantiated one or
whatever else).
More information about the Digitalmars-d-learn
mailing list