Can't understand templates

Sly via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 28 12:36:55 PST 2014


On Friday, 28 November 2014 at 19:45:48 UTC, H. S. Teoh via
Digitalmars-d-learn wrote:

> This syntax is a little confusing, but basically the ":" there 
> is saying
> "this type, when instantiated with the following pattern, 
> produces a
> valid type". Essentially it's equivalent to:
>
> 	Point!T getResponse(T)(string question)
> 		if (is(typeof(Point!T))) // i.e., Point!T is a valid type
> 	{
> 		...
> 	}
>

Is there a typo here? This version doesn't compile with the
original call:     auto pt = getResponse!(Point!int)("point");


> Yes, because D does not support overlapping template overloads.

But I thought the original example has ovelapping templates.
Let's take a simpler example from earlier in the tutorial, where
Point is a non-template struct:


// The general definition of the function template (same as
before)
T getResponse(T)(string question)
{
      writef("%s (%s): ", question, T.stringof);

      T response;
      readf(" %s", &response);

      return response;
}

// The specialization of the function template for Point
T getResponse(T : Point)(string question)
{
      writefln("%s (Point)", question);

      auto x = getResponse!int("  x");
      auto y = getResponse!int("  y");

      return Point(x, y);
}

auto center = getResponse!Point("Where is the center?");

Doesn't getResponse!Point match both declarations?


More information about the Digitalmars-d-learn mailing list