The following fails to compile, with the error "Error: template std.conv.toImpl cannot deduce function from argument types !(S2)(string)" (i.e. S1 works fine, but S2 doesn't):
-----
import std.conv;
unittest {
struct S1 { public string a; }
struct S2 { public string a; this(string s) { a = s; } }
S1 s1 = to!S1("");
S2 s2 = to!S2(""); // <- ERROR
}
-----
Moving the struct definitions out of the unittest block also works fine:
-----
import std.conv;
struct S1 { public string a; }
struct S2 { public string a; this(string s) { a = s; } }
unittest {
S1 s1 = to!S1("");
S2 s2 = to!S2("");
}
-----
This is using DMD v2.067.
Comment #1 by k.hara.pg — 2015-07-05T06:19:42Z
(In reply to Simon Sigurdhsson from comment #0)
> The following fails to compile, with the error "Error: template
> std.conv.toImpl cannot deduce function from argument types !(S2)(string)"
> (i.e. S1 works fine, but S2 doesn't):
S2 is a nested struct and you can create the instance only inside the enclosing unittest, because it requires valid context. Therefore, to!S2 function cannot create it.
> Moving the struct definitions out of the unittest block also works fine:
Moving declarations out of the unittest will make S2 non-nested. Then the limitation will be lifted.
A quick workaround is adding `static` to struct S2.
Comment #2 by robert.schadek — 2024-12-13T18:43:39Z