Bug 14769 – Cannot instantiate templates for locally defined struct when constructor is present

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Mac OS X
Creation time
2015-07-03T21:55:29Z
Last change time
2024-12-13T18:43:39Z
Assigned to
No Owner
Creator
Simon Sigurdhsson
Moved to GitHub: dmd#19011 →

Comments

Comment #0 by Sigurdhsson — 2015-07-03T21:55:29Z
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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19011 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB