Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't deduce template types?

Tags:

d

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?

like image 981
user541686 Avatar asked Mar 22 '26 14:03

user541686


1 Answers

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.

like image 143
Jonathan M Davis Avatar answered Mar 24 '26 04:03

Jonathan M Davis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!