How do I simulate variadic parameters for template (range) functions?
Timon Gehr
timon.gehr at gmx.ch
Wed Aug 24 10:47:25 PDT 2011
On 08/24/2011 07:40 PM, Andrej Mitrovic wrote:
> Here's what I can do with a variadic function:
>
> void main()
> {
> int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];
>
> process(a[a.countUntil(7) .. $]);
> process(1);
> }
>
> void process(int[] vals...)
> {
> foreach (val; vals)
> {
> }
> }
>
> Very simple, pass one or multiple arguments. But then I thought about
> using the `until` template instead of countUntil. However `until`
> returns a range. So my next guess was to write:
>
> void main()
> {
> int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];
>
> process(a.until(7)); // ok
> process(4); // error since 4 is not a range
> }
>
> void process(Range)(Range vals) if (isInputRange!Range&&
> is(ElementType!Range == int))
> {
> foreach (val; vals)
> {
> }
> }
>
> Is it somehow possible to automatically convert a literal to a range?
> I really miss the convenience of variadic functions. I thought about
> making an overload that only takes an int and constructing a simple
> input range around it so it can be passed to process(), e.g.:
>
> void process(Range)(Range vals) if (isInputRange!Range&&
> is(ElementType!Range == int))
> {
> foreach (val; vals)
> {
> }
> }
>
> void process(int arg)
> {
> process(makeInputRange(arg)); // make an input range, pass to
> above process()
> }
>
> But I can't overload templated and non-templated functions, I think
> this is one of those old-standing bugs.
Workaround: You can make it a templated function with no template
arguments and wrap a non-templated version, if it is important that the
implementation is not a template.
More information about the Digitalmars-d-learn
mailing list