Constraints

Ibrahim Gokhan YANIKLAR yanikibo at gmail.com
Thu May 10 14:48:27 PDT 2012


>
> void insertFront(U : T)(U item) { ... }
>
> void insertFront(Range : CBidirectionalRange)(Range r)
> 	if(isImplicitlyConvertible!(ElementType!Range, T)) { ... }
>
> void insertFront(Range : CInputRange)(Range r)
> 	if(isImplicitlyConvertible!(ElementType!Range, T)) { ... }
>

An alternative solution:


concept CInputRange(R, E, T = E)
{
	static assert (isDefinable!R);
	static assert (isRange!R);
	bool empty();
	void popFront();
	E front();
	static assert (isImplicitlyConvertible!(E, T));
}

concept CForwardRange(R, E, T = E) : CInputRange!(R, E, T)
{
	R save();
}

concept CBidirectionalRange(R, E, T = E) : CForwardRange!(R, E, T)
{
	void popBack();
	E back();
}

concept CRandomAccessRange(R, E, T = E) : CBidirectionalRange!(R, 
E, T)
	if (!isInfinite!R)
{
	static assert (is(typeof(R.init[1])));
	static assert(hasLength!R);
	static assert(!isNarrowString!R);
}

concept CRandomAccessRange(R, E, T = E) : CForwardRange!(R, E, T)
	if (isInfinite!R)
{
	static assert (is(typeof(R.init[1])));
}


Then we can use this concepts in your functions like:


void insertFront(U : T)(U item) { ... }

void insertFront(Range : CBidirectionalRange!T)(Range r) { ... }

void insertFront(Range : CInputRange!T)(Range r) { ... }

The rule is simple:
Do not specify the first parameter (e.g. "R": reserved for 
typeof(this)) and the automatically deduced parameters (e.g. "E": 
deduced from return type of "front()").





More information about the Digitalmars-d mailing list