I'm writing 2 overloads of a function, both of them with variadic template compile-time parameters. One should take symbols as the templates, the other strings. I want to constrain the template instantiation to these two cases. The best I came up with was this:
bool func(SYMBOLS...)() if(!is(typeof(SYMBOLS[0]) == string)) {
}
and
bool func(STRINGS...)() if(is(typeof(STRINGS[0]) == string)) {
}
Obviously this only checks the first template parameter, and while it works given the code I've written so far, I wish I could say "only for all strings" and "only for not all strings". Is there a way?
It took me a while to figure it out, but here is a potential solution to your problem:
module main;
import std.stdio;
int main(string[] argv)
{
bool test1PASS = func(1,2,3,4,5.5, true);
//bool test2CTE = func(1,2,3,4,5, true, "CRAP");
bool test3PASS = func("CRAP", "3", "true");
//bool test4CTE = func("CRAP", "3", "true", 42, true, 6.5);
return 0;
}
bool func(NOSTRINGS...)(NOSTRINGS x)
if ({foreach(t; NOSTRINGS) if (is(t == string)) return false; return true; }()) {
// code here ...
return true;
}
bool func(ONLYSTRINGS...)(ONLYSTRINGS x)
if ({foreach(t; ONLYSTRINGS) if (!is(t == string)) return false; return true; }()) {
// code here ...
return true;
}
This works (from http://forum.dlang.org/thread/[email protected] with help from Andrej Mitrovic):
import std.traits;
import std.typetuple;
void runTests(SYMBOLS...)() if(!anySatisfy!(isSomeString, typeof(SYMBOLS))) {
}
void runTests(STRINGS...)() if(allSatisfy!(isSomeString, typeof(STRINGS))) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With