Can't use variadic arguments to functions that use templates
JS
js.mdnq at gmail.com
Sat Jul 20 09:29:21 PDT 2013
variadic parameters are suppose to make life easier but it seems
those don't work with templates.
template A(int L) { ... }
void foo(T)(string s, T t...)
{
A!(t.length);
}
gives error t can't be read at compile time.
I understand in general why this works BUT I will only ever use
foo where the number of arguments are known at compile time.
Therefor to get the code to work I have to revert back to the old
way of overloading foo many times to emulate variadic parameters.
Essentially all I want to do is to use foo like
foo("a", "b", "c", 'd', s, ...) but the number of arguments is
fixed at compile time(almost all arguments are, aren't they?).
Here is a challenge:
Write a function that accepts any number of string or character
variables or literals and returns the concatenates them
efficiently as possible(as efficient as fixed arguments).
e.g.,
foo1(string s) { return s; }
foo2(string s1, string s2) { return s1~s2; }
etc...
if we had compile time variadic parameters, we could do something
like
foo(T t$...) { return mixin(concat!t); } where concat produces
the string t[0]~t[1]~t[2]... which produces the same code as the
overloaded case, but must simpler.
I don't think this is possible because there is no static version
of variadic arguments, i.e., one who's length is always known at
compile time.
More information about the Digitalmars-d-learn
mailing list