I have a class which is generated from a tool with member variable names that vary. I would like to use a single templated class to access these members. I can do this in the following way by using a non-type member pointer:
template<typename MODULE,
int MODULE::*a> class Foo {
public:
Foo() {
MODULE mod;
std::cout << mod.*a;
}
};
struct Bar {
int random_name01234{20};
};
int main(int argc, char** argv, char** env) {
Foo<Bar, &Bar::random_name01234> foobar;
}
However, the generated class (Bar in this example) uses references to members I do not have access to. I cannot pass a reference as a non-type template parameter as described here - Reference as a non-type template argument:
template<typename MODULE,
int& MODULE::*a> class Foo {
public:
Foo() {
MODULE mod;
std::cout << mod.*a;
}
};
class Bar {
private:
int random_name01234{20};
public:
int& random_name01234_ref{random_name01234};
};
int main(int argc, char** argv, char** env) {
Foo<Bar, &Bar::random_name01234_ref> foobar;
}
error: cannot create pointer to reference member ‘Bar::random_name01234_ref’
Is there another way I can approach this to pass the random member names to a templated function?