Variadic template with template arguments in pairs
Anonymouse
asdf at asdf.net
Wed Sep 12 15:12:16 UTC 2018
I'm trying to create a variadic template function that takes
pairs of arguments. Sort of like getopt, I want to pass any
number of pairs of a string and some pointer. Or any size chunk
larger than one.
Something like the following, assuming the existence of a
hypothetical template pairwise:
---
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);
}
}
bool b1 = true;
bool b2 = false;
string s = "some string";
int i = 42;
doByPair("foo", &b1, "bar", &b2, "baz", &s, "qux", &i);
---
Should output:
bool foo: true
bool bar: false
string baz: some string
int qux: 42
What is the right way to go about doing this?
More information about the Digitalmars-d-learn
mailing list