D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5843 - Unable to convert a struct with an alias-this to long/ulong to int, using std.conv.to!int.
Summary: Unable to convert a struct with an alias-this to long/ulong to int, using std...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: Other Mac OS X
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2011-04-13 09:03 UTC by kennytm
Modified: 2012-04-23 10:42 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description kennytm 2011-04-13 09:03:00 UTC
Test case:

-----
import std.stdio;

struct S {
    ulong a;
    alias a this;
}

void main() {
    writeln(S.init);
}
-----

As of 2.052, this code generates compile-time error with


/usr/include/phobos/std/conv.d(99): Error: template std.conv.toImpl(T,S) if (!implicitlyConverts!(S,T) && isSomeString!(T) && isInputRange!(Unqual!(S)) && isSomeChar!(ElementType!(S))) does not match any function template declaration
/usr/include/phobos/std/conv.d(99): Error: template std.conv.toImpl(T,S) if (!implicitlyConverts!(S,T) && isSomeString!(T) && isInputRange!(Unqual!(S)) && isSomeChar!(ElementType!(S))) cannot deduce template function from argument types !(int)(S)
/usr/include/phobos/std/conv.d(99): Error: template instance errors instantiating template
/usr/include/phobos/std/format.d(1630): Error: template instance std.conv.to!(int).to!(S) error instantiating
/usr/include/phobos/std/format.d(335):        instantiated from here: getNthInt!(S)
/usr/include/phobos/std/stdio.d(660):        instantiated from here: formattedWrite!(LockingTextWriter,immutable(char),S)
/usr/include/phobos/std/stdio.d(1493):        instantiated from here: write!(S,char)
x.d(9):        instantiated from here: writeln!(S)
/usr/include/phobos/std/format.d(335): Error: template instance std.format.getNthInt!(S) error instantiating
/usr/include/phobos/std/stdio.d(660):        instantiated from here: formattedWrite!(LockingTextWriter,immutable(char),S)
/usr/include/phobos/std/stdio.d(1493):        instantiated from here: write!(S,char)
x.d(9):        instantiated from here: writeln!(S)
Comment 1 kennytm 2011-06-02 10:28:36 UTC
The underlying problem is to!int of the struct isn't defined.

-------------------------
import std.conv;
struct SSSS {
    long a;
    alias a this;
}
void main() {
    to!int(SSSS.init);
}
-------------------------
/usr/include/phobos/std/conv.d(99): Error: template std.conv.toImpl(T,S) if (!implicitlyConverts!(S,T) && isSomeString!(T) && isInputRange!(Unqual!(S)) && isSomeChar!(ElementType!(S))) does not match any function template declaration
/usr/include/phobos/std/conv.d(99): Error: template std.conv.toImpl(T,S) if (!implicitlyConverts!(S,T) && isSomeString!(T) && isInputRange!(Unqual!(S)) && isSomeChar!(ElementType!(S))) cannot deduce template function from argument types !(int)(SSSS)
/usr/include/phobos/std/conv.d(99): Error: template instance errors instantiating template
x.d(7): Error: template instance std.conv.to!(int).to!(SSSS) error instantiating
-------------------------
Comment 2 SomeDude 2012-04-23 09:06:43 UTC
Both examples compile and run with 2.059 Win32.
The first one displays 0.
Comment 3 Kenji Hara 2012-04-23 10:42:36 UTC
In 2.059, std.format module treats alias this type properly.

It was fixed by the pull request:
https://github.com/D-Programming-Language/phobos/pull/488