Variadic template with template arguments in pairs
Paul Backus
snarwin at gmail.com
Wed Sep 12 21:33:17 UTC 2018
On Wednesday, 12 September 2018 at 15:12:16 UTC, Anonymouse wrote:
> void doByPair(Args...)(Args args)
> if (Args.length)
> {
> foreach (pair; args.pairwise)
> {
> static assert(is(typeof(pair[0]) == string));
> static assert(isPointer!(pair[1]));
> assert(pair[1] !is null);
>
> string desc = pair[0];
> auto value = *pair[1];
> writefln("%s %s: %s", typeof(value).stringof, desc,
> value);
> }
> }
The easiest way is probably to iterate using indices with an
increment of 2, e.g.:
static foreach(i; iota(0, args.length, 2))
{
static assert(is(typeof(args[i]) == string));
static assert(isPointer!(args[i+1]));
// etc.
}
Another alternative is to write the function recursively:
void doByPair(T, Rest...)(string desc, T* valuePtr, Rest rest)
{
writefln("%s %s: %s", T.stringof, desc, *valuePtr);
if (rest.length) doByPair(rest);
}
More information about the Digitalmars-d-learn
mailing list