simple (I think) eponymous template question ... what is proper idimatic way ?
james.p.leblanc
james.p.leblanc at gmail.com
Tue Aug 17 20:29:51 UTC 2021
On Tuesday, 17 August 2021 at 20:13:59 UTC, Paul Backus wrote:
> FYI: in this particular case, you can use
> std.meta.staticIndexOf instead of writing the recursion out
> yourself:
>
> import std.meta: staticIndexOf;
> enum isAmong(T, S...) = staticIndexOf!(T, S) >= 0;
>
> Docs: https://phobos.dpldocs.info/std.meta.staticIndexOf.html
All, Thanks again ... Paul, I will try this staticIndexOf as you
mention.
I had been receiving "circular reference to variable" error
messages from the
"isAmong" suggestion. (But, I very likely had misunderstood how
to use this.)
I would still be interested to learn what I have done wrong. So,
below
is my code:
import std.stdio;
import std.meta : AliasSeq;
template isAmong(T, S...) {
static if (S.length == 0)
enum isAmong = false;
else
enum isAmong = is(T == S) || isAmong(T, S[1..$]);
}
alias MyTypes = AliasSeq!(int, float);
auto myFunc(T)(T a, T b) if (isAmong!(T, MyTypes)) {
writeln(" in myFunc ");
return;
}
void main(){
writeln("started ...");
auto a = 1;
auto b = 2;
myFunc!(int)(a, b);
return;
}
And, here are the error message:
(master) Notes > dmd recursive_template.d
recursive_template.d(9): Error: circular reference to variable
`recursive_template.isAmong!(int, int, float).isAmong`
recursive_template.d(17): Error: template instance
`recursive_template.isAmong!(int, int, float)` error instantiating
recursive_template.d(29): while looking for match for
`myFunc!int`
Can anyone see what is going on?
Best Regards,
James
More information about the Digitalmars-d-learn
mailing list