Typed variadic template syntax?
bearophile
bearophileHUGS at lycos.com
Tue Jan 28 16:14:30 PST 2014
This is not an enhancement request, but it presents a potential
enhancement.
This is C++11 code:
template<int... digits> struct value;
template<> struct value<> {
static const int v = 0;
};
template<int first, int... rest> struct value<first, rest...> {
static int const v = 10 * value<rest...>::v + first;
};
You can translate it to D like this:
enum isInt(T) = is(T == int);
template value(xs...) if (allSatisfy!(isInt, xs)) {
static if (xs.length == 0)
enum value = 0;
else
enum value = xs[0] + 10 * value!(xs[1 .. $]);
}
This D code:
template value(xs...) if (allSatisfy!(isInt, xs)) {
Is slower to compile than this D code:
template value(xs...) {
So we could allow D code like:
template value(int[] xs...) {
I think it could be faster than using "if (allSatisfy!(isInt,
xs))" and it's nicer looking.
On the other hand I don't know how much common are template
instantiations with values all of the same type (like all ints as
in this case) in D code.
Opinions welcome.
Bye,
bearophile
More information about the Digitalmars-d
mailing list