simple (I think) eponymous template question ... what is proper idimatic way ?

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Aug 17 19:44:29 UTC 2021


On Tue, Aug 17, 2021 at 07:22:54PM +0000, james.p.leblanc via Digitalmars-d-learn wrote:
[...]
> auto moo(T : (int || float || mySpecialStruct )(T myMoo) {•••}
> 
> When re-using any such sets, it would be nice to define the set as
> follows:
> 
> S = (int || float || mySpecialStruct)
> 
> and then define "moo" more concisely as:
> 
> auto moo(T < S)(T myMoo) {•••}
> 
> ( where I have used "<" to mean "T is a member of S").
[...]

You could use a helper template and an AliasSeq for this:

	template isAmong(T, S...) {
		static if (S.length == 0)
			enum isAmong = false;
		else
			enum isAmong = is(T == S) ||
				isAmong(T, S[1..$]);
	}

	import std.meta : AliasSeq;
	alias MyTypes = AliasSeq!(int, float, MySpecialStruct);

	auto myFunc(T)(T a, T b) if (isAmong!(T, MyTypes)) { ... }


T

-- 
I am a consultant. My job is to make your job redundant. -- Mr Tom


More information about the Digitalmars-d-learn mailing list