Gzp wrote:
void foo(T)(ref T t) if (isPrime!(T)) { ... }
void foo(T)(ref T t) if (!isPrime!(T)) { ... }
What is the difference b/n
void foo(T)(ref T t) if (isPrime!(T)) { ... }
and
void foo(T)(ref T t) static if (isPrime!(T)) { ... }
as both of them seems to be a compile time check for me.
I'd say that the main difference is that the last one doesn't compile.
:) It's simply not valid D code.
The if clause in a template declaration is used for template parameter
matching (like in Bill's examples), whereas static if is a statement
that is used for conditional compilation. However, you can achieve more
or less the same thing with them both, but in slightly different ways.
// This function template is instantiated when T is int.
void foo(T)(T t) if (is(T == int)) { ... }
// This function template is instantiated for all other types
void foo(T)(T t) if (!is(T == int)) { ... }
With static if you'd do it like this:
void foo(T)(T t)
{
static if (is(T == int))
{
// This code is compiled when T is int.
...
}
else
{
// This code is compiled for all other types.
...
}
}
Be aware that if you are chaining several static ifs, you have to type
"static" for each one:
static if (foo) { ... }
else static if (bar) { ... }
else static if (baz) { ... }
else { ... }
-Lars