mercredi 22 août 2018

function parameter pack for member variable pointer

I am trying to access variables in a struct thru nested member pointers:

#include <iostream>

typedef struct {
    int a;
    int b;
} bar;

typedef struct {
    int c;
    bar d;
} baz;

template <typename obj, class C1, class C2, typename T1, typename T2>
T2 test1(const obj& obj_, T1 C1::* field1_, T2 C2::* field2_)
{
    return (obj_.*field1_).*field2_;
}

int main()
{
    baz myObj;
    test1(myObj, &baz::d, &bar::b);
}

How would I turn the function test into a variadic function, so that i can access variables at variable "depths" into the struct? I've tried to follow the second example in the Function parameter list section here, but am not getting it it seems:

template <typename obj, class ...C, typename... T>
void test2(const obj& obj_, T C...::* field_)
{
    // ??
    // and what about the function return parameter?
}

int main()
{
    baz myObj;
    test2(obj,&baz::d,&bar::b);
    test2(obj,&baz::c);
}

With this, the definition of test2() already doesn't compile.

Aucun commentaire:

Enregistrer un commentaire