Variadic templates
Kirk McDonald
kirklin.mcdonald at gmail.com
Thu Nov 2 16:31:31 PST 2006
Kirk McDonald wrote:
> Walter Bright wrote:
>> See http://www.digitalmars.com/d/variadic-function-templates.html
>>
>> Why now? Because it's such a pain to do template programming without
>> them, and because I wanted to have a good signals and slots
>> implementation. That was the last piece needed to make S&S work right
>> (unless I'm way off track with it).
>>
>> There's a lot of unexplored territory with the tuples, they should be
>> able to do a lot more than the current rather limited ability.
>
> This is great! I've already thought of a number of ways to vastly
> improve the metaprogramming stuff I'm using in Pyd. (Sigh... and I just
> re-wrote all of that once already.) Pyd is a great use-case for an MPL,
> and I've been compiling a pretty decent one while writing it (with great
> work from h3r3tic and Don and others). I think it's about to get a whole
> lot more interesting...
>
In trying to work with the variadic templates, I've discovered a few
weaknesses and bugs.
1) Index in static foreach is not const
Take this template function:
void foo(T ...)(T t) {
foreach (i, arg; t) {
t[i] = something_else;
}
}
>dmd test
test.d(35): Integer constant expression expected instead of cast(int)(i)
Tuples can only be indexed by const (compile-time) index values. The
index form of foreach results in runtime indexes. This inconsistency
prevents the user from altering the tuple from inside a loop. This
prevents some exceedingly useful constructions.
Just as good would be the ability to declare "arg" as inout, as in:
void foo(T ...)(T t) {
foreach (inout arg; t) {
arg = something_else;
}
}
DMD currently barfs on this.
2) Tuple aliasing is buggy
I'll let the example speak for itself.
template Foo(T ...) {
alias T Foo;
}
void func(T ...)(T t) {
foreach (arg; t) {
writefln(arg);
}
}
void main() {
alias Foo!(int, real, char[]) F;
func!(F)(12, 3.8, "hello");
}
>dmd test2
Assertion failure: 'global.errors' on line 2752 in file 'template.c'
abnormal program termination
I had other complaints, but these are the only ones that occur to me at
the moment.
--
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org
More information about the Digitalmars-d
mailing list