passing a variadic parameter to randomSample

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jan 25 23:03:41 UTC 2022


On Tue, Jan 25, 2022 at 10:48:26PM +0000, forkit via Digitalmars-d-learn wrote:
[...]
> ... but my main focus here, was learning about variadic template
> functions.

D has several flavors of variadics:

1) C-style variadics (not type-safe, not recommended):

	int func(int firstArgc, ...)

2) D-style type-safe variadics (non-templated):

	int func(int[] args...)

   All arguments must be of the same type, and the function receives
   them as an array of that type, so .length can be used to ensure you
   don't overrun the array.

3) Variadic template functions:

	int func(Args...)(Args args)

   Arguments can be of any type, the type can be obtained with Args[i].
   (Not to be confused with lowercase args[i], which gives you the
   argument value itself.)  The most flexible of the lot, but may also
   lead to template bloat if used excessively (one instantiation is
   generated for every different combination of argument types).

There's also the lazy variant of (2) with delegates, but I've never used
them before.


T

-- 
An elephant: A mouse built to government specifications. -- Robert Heinlein


More information about the Digitalmars-d-learn mailing list