How do I specify a variadic template constraint on all the whole tuple?
Andrej Mitrovic
andrej.mitrovich at gmail.com
Thu Jul 18 06:25:54 PDT 2013
On Thursday, 18 July 2013 at 13:18:29 UTC, Atila Neves wrote:
> bool func(SYMBOLS...)() if(!is(typeof(SYMBOLS[0]) == string)) {
> }
> and
>
> bool func(STRINGS...)() if(is(typeof(STRINGS[0]) == string)) {
> }
Here you go:
-----
import std.string;
import std.traits;
import std.typetuple;
import std.functional;
bool func(Symbols...)(Symbols symbols)
if (!anySatisfy!(isSomeString, Symbols))
{
return true;
}
bool func(Strings...)(Strings strings)
if (allSatisfy!(isSomeString, Strings))
{
return true;
}
void main()
{
func("1", "2");
func(1, 2);
static assert(!__traits(compiles, func(1, "2")));
}
-----
And remember you have to actually list the runtime arguments
after the type arguments (e.g. 'Strings strings'), otherwise the
template won't match.
More information about the Digitalmars-d-learn
mailing list