www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Variadic function with parameters all of a specific type

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
I want to create a function that takes a variadic number of 
arguments all of a specific type, say T, without having to create 
GC-allocated heap array.

Is there a better way than:

f(Args...)(Args args)
     if (allSameType!(Args, T);

in terms of template bloat?
Jun 17 2016
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 17.06.2016 23:00, Nordlöw wrote:
 I want to create a function that takes a variadic number of arguments
 all of a specific type, say T, without having to create GC-allocated
 heap array.

 Is there a better way than:

 f(Args...)(Args args)
      if (allSameType!(Args, T);

 in terms of template bloat?
alias T=int; void foo(T[] a...) nogc{} void bar() nogc{ foo(1,2,3); }
Jun 17 2016
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Friday, 17 June 2016 at 21:20:01 UTC, Timon Gehr wrote:
 On 17.06.2016 23:00, Nordlöw wrote:
 I want to create a function that takes a variadic number of 
 arguments
 all of a specific type, say T, without having to create 
 GC-allocated
 heap array.

 Is there a better way than:

 f(Args...)(Args args)
      if (allSameType!(Args, T);

 in terms of template bloat?
alias T=int; void foo(T[] a...) nogc{} void bar() nogc{ foo(1,2,3); }
this. the compiler is smart, it is creating a slice of stack memory here. note that you can't just assign `a` to, for example, global or member: when execution of `foo` ends, `a` will still point to stack memory. if you need to store `a` somewhere, be sure to `.dup` it.
Jun 17 2016
prev sibling parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Friday, 17 June 2016 at 21:20:01 UTC, Timon Gehr wrote:
 void foo(T[] a...) nogc{}

 void bar() nogc{
     foo(1,2,3);
 }
Ahh, cool. I was completely unaware of this feature. Doc here: https://dlang.org/spec/function.html#typesafe_variadic_functions
Jun 18 2016