Can't understand templates

Sly via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 29 11:47:43 PST 2014


This is clearly an incorrect way because it requires editing the
original definition every time a new class is introduced.

On Saturday, 29 November 2014 at 19:10:34 UTC, Meta wrote:
> On Saturday, 29 November 2014 at 18:19:40 UTC, Sly wrote:
>> You miss another definition which introduces a conflict:
>> T getResponse(T)(string question)
>> {...}
>
> In that case, you're better off with a pair of declarations:
>
> struct Point(T)
> {
> 	T x;
> 	T y;
> }
>
> T getResponse(T)(string message)
> if (!is(T == Point!U, U))
> {
> 	return T.init;
> }
>
> Point!T getResponse(T)(string message)
> if (is(T == Point!U, U))
> {
> 	return Point!T(T.init, T.init);
> }
>
> void main()
> {
> 	auto t = getResponse!int("test");
> 	auto p = getResponse!(Point!int)("test");
> }


More information about the Digitalmars-d-learn mailing list