DIP Mir1 Draft: Variadic template parameters with the same time.

ag0aep6g via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 29 15:03:36 PDT 2016


On 09/29/2016 11:28 PM, Ilya Yaroshenko wrote:
> On Thursday, 29 September 2016 at 20:57:00 UTC, ag0aep6g wrote:
[...]
>> void foo(size_t n)(size_t[n] a ...) { /* ... */ }
>
> Just found an example, where this approach does not work :-(
>
> template transposed(Dimensions...)
>     if (Dimensions.length)
> {
>     Slice!(N, Range) transposed(size_t N, Range)(auto ref Slice!(N,
> Range) slice)
>     {
>         ...
>     }
> }

When the values themselves are known at compile time, we can convert 
them to size_t before generating the function:

----
enum isIndex(T) = is(T == size_t); /* by pineapple */
enum asIndex(size_t value) = value;

template transposed(Dimensions...)
     if (Dimensions.length)
{
     import std.meta: allSatisfy, staticMap;
     static if (allSatisfy!(isIndex, typeof(Dimensions)))
     {
         void transposed() {} /* simplified for the example */
     }
     else
     {
         alias transposed = transposed!(staticMap!(asIndex, Dimensions));
     }
}

alias t1 = transposed!(1, 2, 3);
alias t2 = transposed!(1LU, 2LU, 3LU);

static assert(&t1 == &t2); /* passes */
----


More information about the Digitalmars-d mailing list