CT-String as a Symbol

Vlad Levenfeld via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 21 01:15:04 PDT 2015


On Tuesday, 21 April 2015 at 08:09:38 UTC, Per Nordlöw wrote:
> On Tuesday, 21 April 2015 at 07:46:03 UTC, Vlad Levenfeld wrote:
>> template dimensionality (S) {
>>  template count_dim (uint i = 0) {
>>    static if (is (typeof(S.init.opSlice!i (0,0))))
>>      enum count_dim = count_dim!(i+1);
>>    else enum count_dim = i;
>>  }
>>
>>  alias dimensionality = count_dim!();
>> }
>>
>> Then you throw in some more stuff to detect 1-dimensional 
>> cases.
>
> Great! I guess a template restriction on opSlice would be in 
> Place aswell.
>
> One thing: Why aren't you using opIndex instead? If there are 
> types that have opIndex but not opSlice defined then 
> dimensionality() could cover more types, right?

So, if the type is multidimensional, that means its using the new 
opIndex/opSlice syntax that was designed for multidimensional 
structures. So I know that, if dim > 1, opSlice!i must be 
defined, and (assuming we are only working with integral indices 
here) I can instantiate it with opSlice!i (0,0).

For other types, you would throw in a test for a length member, 
or for front, and know that you have a 1-dimensional type.

Alternatively, if we're assuming only integral indices, a 
recursive attempt to get the typeof(opIndex (Repeat!(i, 0))) 
would probably handle all the opIndex-but-not-opSlice cases (but 
you still need to handle non-indexed ranges and D arrays 
specially).


More information about the Digitalmars-d-learn mailing list