Varargs and default arguments
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Oct 6 13:27:29 PDT 2015
On Tuesday 06 October 2015 22:01, Nick Sabalausky wrote:
> Ok, D-style varargs can accept a parameter length of zero:
>
> ---------------
> void foo(T...)(T args) {
> //...
> }
> foo();
> foo(t1, t2);
> ---------------
Terminology fun:
The spec uses the term "D-style variadic function" for something else yet:
`int abc(char c, ...);` -- http://dlang.org/function.html#variadic
Yours is technically a "variadic function template", I guess, i.e., a
function template that's variadic, not a template of a variadic function.
> Is there any way to stick a param with a default value before that?
>
> ---------------
> void foo(T...)(string str=null, T args=/+what goes here???+/) {
> //...
> }
> foo();
> foo(str);
> foo(str, t1, t2);
> ---------------
>
> Obviously this can be worked around easily enough by splitting it into
> two overloads ("foo(string str=null)" and "foo(string str, T args)"),
> but is there a way to do it in one?
You can put an expression tuple ("expression AliasSeq"??) there. T.init is
one that always fits T's types. But you could generate one with different
values, too.
----
void foo(T...)(string str=null, T args = T.init) {
//...
}
void main()
{
foo();
foo("");
foo("", 1, 2);
}
----
More information about the Digitalmars-d-learn
mailing list