Template function type inference with default arguments
ixid via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jan 3 16:22:00 PST 2015
Why don't templates take a type from the default argument if
nothing else is supplied? It would be useful to be able to use an
enum to set a default.
enum MAX = 1_000;
auto sieve(T)(T max = MAX) {
import std.bitmanip : BitArray;
BitArray n;
n.length = max;
T[] primes = [2];
for(T i = 3; i < max; i += 2)
if(n[i] == 0) {
primes ~= i;
for(T j = i + i; j < max; j += i)
n[j] = 1;
}
return primes;
}
Changing the type to T = typeof(MAX) works but feels
unnecessarily clunky and out of step with how templates normally
operate when supplied with an argument.
More information about the Digitalmars-d-learn
mailing list