Search code examples
d

Can't deduce template types?


So I have the following code:

void invert(T)(T[2][] arr)
{
    auto result = new T[2][arr.length];
    foreach (i, v; arr)
        result[i] = [-v[0], -v[1]];
    return result;
}

and I call it:

invert([[5, 6], [6, 7]]);

and I get:

test.d(94): Error: template test.invert(T) does not match any function template declaration
test.d(94): Error: template test.invert(T) cannot deduce template function from argument types !()(int[][])

What's the easiest way to fix this without losing the auto-inference feature?


Solution

  • The problem is that you can't have a literal which is a static array. You end up with a dynamic array - int[][] in this case - instead of the int[2][] that you want. The inference works just fine. It's the type that you're giving it which is wrong. You're going to have to create a variable of the correct type.