Can't understand templates

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 29 11:10:32 PST 2014


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