Given POD structures of the general form
struct case_0 { const char *foo; };
struct case_1i { const char *foo; int v0; };
struct case_1d { const char *foo; double v0; };
struct case_2ii { const char *foo; int v0; int v1; };
struct case_2id { const char *foo; int v0; double v1; };
// etc
is it possible to dispatch to (template) members of a function overload set based on the presence or absence of the v0, v1, etc data members — ideally, without any dependence on the specific type of these members — and if so, how? Concretely, given
void
process(const case_0& c)
{
do_stuff_with(c.foo);
}
template <typename case_1> void
process(const case_1& c)
{
do_stuff_with(c.foo, c.v0);
}
template <typename case_2> void
process(const case_2& c)
{
do_stuff_with(c.foo, c.v0, c.v1);
}
I would like each overload to be selected for all case_* structures that have all the v-members that are used within its body, and — equally important — don’t have any v-members that are not used within its body.
This program must be 100% self-contained, so no Boost, please. C++11 features are okay.
You need to write a set of traits such as
has_v0andhas_v1(which I’m sure has been demonstrated many times on SO) then constrain your overloads using them:You can simplify the constraints with something like
e.g.