Can't understand templates
Sly via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Nov 29 12:50:10 PST 2014
OK. I'm using gdc 4.8.2 and this doesn't compile, so it's
probably an old version. Checked on ideone and it works with
dmd-2.042.
Thanks a lot for your help!
On Saturday, 29 November 2014 at 20:16:24 UTC, Ali Çehreli wrote:
> On 11/29/2014 10:19 AM, Sly wrote:
>
> > You miss another definition which introduces a conflict:
> > T getResponse(T)(string question)
> > {...}
>
> The following works with dmd git head:
>
> import std.stdio;
>
> T getResponse(T)(string question)
> {
> writef("%s (%s): ", question, T.stringof);
>
> T response;
> readf(" %s", &response);
>
> return response;
> }
>
> class Pair(A, B) {
> A a;
> B b;
> this(A a, B b) {this.a = a; this.b = b;}
> }
>
> Pair!(A, B) getResponse(P : Pair!(A, B), A, B)(string question)
> {
> writeln(question);
> auto a = getResponse!A(" a");
> auto b = getResponse!B(" b");
> return new Pair!(A, B)(a, b);
> }
>
> struct Point(T)
> {
> T x;
> T y;
> }
>
> Point!T getResponse(P : Point!T, T)(string question)
> {
> writefln("%s (Point!%s)", question, T.stringof);
>
> auto x = getResponse!T(" x");
> auto y = getResponse!T(" y");
>
> return Point!T(x, y);
> }
>
> void main()
> {
> auto number = getResponse!int("number?");
> writeln(number);
>
> auto pair = getResponse!(Pair!(int, long))("pair?");
> writeln(pair);
>
> auto point = getResponse!(Point!int)("point?");
> writeln(point);
> }
>
> Ali
More information about the Digitalmars-d-learn
mailing list