Cannot always deduce template arguments when using implicitly cast array literals
HuskyNator
HuskyNator at protonmail.ch
Thu Apr 21 15:59:24 UTC 2022
On Friday, 23 July 2021 at 13:53:27 UTC, Rekel wrote:
> As one can see, implicitly casting array literals in templates
> works fine in the case of bar, as does explicit use of
> templates in the case of foo, but for some reason foo does not
> manage to deduce its arguments like bar does.
Interestingly enough, I ran into the same issue recently, but
with a much simpler piece of code:
```d
module app;
import std.stdio;
struct One(T, uint L) {
T[L] array;
bool opEquals(T2)(const T2[L] rhs) const {
return true;
}
}
struct Two(T, uint L) {
T[L][L] array;
bool opEquals(T2)(const T2[L][L] rhs) const {
return true;
}
}
void foo() {
auto a = One!(float, 2)([1, 2]);
int[2] b = [1, 2];
writeln(a == b); // True
writeln(a.array == b); // True
writeln(a == [1, 2]); // True
writeln(a.array == [1, 2]); // True
}
void bar() {
auto a = Two!(float, 2)([[1, 2], [3, 4]]);
int[2][2] b = [[1, 2], [3, 4]];
writeln(a == b); // True
writeln(a.array == b); // True
writeln(a == [[1, 2], [3, 4]]); // cannot deduce function from
argument types `!()(int[][])`
writeln(a.array == [[1, 2], [3, 4]]); // True
}
void main(){
foo();
bar();
}
```
I'd love to know if there's any plans on changing/fixing this. I
haven't been able to find any mention of it apart from in this
thread.
Kind regards,
~ HN
(/Rekel: name change & account loss. It'd be nice if the forum
supported editing & profiles (including posts/comments or at
least login credential retrieval (like https://code.dlang.org/
does))
More information about the Digitalmars-d-learn
mailing list