#include <vector>
template <typename TopContainer, typename TopElement, typename BottomContainer>
void genericFoo(const TopContainer& topList, BottomContainer TopElement::* bottomList)
{
for (auto it = topList.begin(); it != topList.end(); ++it) {
// ...
const BottomContainer& list2 = (*it).*bottomList;
for (auto it2 = list2.begin(); it2 != list2.end(); ++it2) {
// do some stuff
}
}
}
struct listA {
std::vector<char> listOfApples;
};
struct listB {
std::vector<int> listOfToys;
};
std::vector<listA> listOfItems1;
std::vector<listB> listOfItems2;
int main()
{
std::vector<int> listB::*pList = &listB::listOfToys;
genericFoo(listOfItems2, pList);
return 0;
}
I2luY2x1ZGUgPHZlY3Rvcj4KCnRlbXBsYXRlIDx0eXBlbmFtZSBUb3BDb250YWluZXIsIHR5cGVuYW1lIFRvcEVsZW1lbnQsIHR5cGVuYW1lIEJvdHRvbUNvbnRhaW5lcj4Kdm9pZCBnZW5lcmljRm9vKGNvbnN0IFRvcENvbnRhaW5lciYgdG9wTGlzdCwgQm90dG9tQ29udGFpbmVyIFRvcEVsZW1lbnQ6OiogYm90dG9tTGlzdCkgCnsKICAgIGZvciAoYXV0byBpdCA9IHRvcExpc3QuYmVnaW4oKTsgaXQgIT0gdG9wTGlzdC5lbmQoKTsgKytpdCkgewogICAgICAgIC8vIC4uLgogICAgICAgIGNvbnN0IEJvdHRvbUNvbnRhaW5lciYgbGlzdDIgPSAoKml0KS4qYm90dG9tTGlzdDsKICAgICAgICBmb3IgKGF1dG8gaXQyID0gbGlzdDIuYmVnaW4oKTsgaXQyICE9IGxpc3QyLmVuZCgpOyArK2l0MikgewogICAgICAgICAgICAvLyBkbyBzb21lIHN0dWZmCiAgICAgICAgfQogICAgfQp9CgpzdHJ1Y3QgbGlzdEEgewogICAgc3RkOjp2ZWN0b3I8Y2hhcj4gbGlzdE9mQXBwbGVzOyAgICAKfTsKCnN0cnVjdCBsaXN0QiB7CiAgICBzdGQ6OnZlY3RvcjxpbnQ+IGxpc3RPZlRveXM7Cn07CgpzdGQ6OnZlY3RvcjxsaXN0QT4gbGlzdE9mSXRlbXMxOwpzdGQ6OnZlY3RvcjxsaXN0Qj4gbGlzdE9mSXRlbXMyOwoKICAgIAppbnQgbWFpbigpCnsKICAgIHN0ZDo6dmVjdG9yPGludD4gbGlzdEI6OipwTGlzdCA9ICZsaXN0Qjo6bGlzdE9mVG95czsKICAgIGdlbmVyaWNGb28obGlzdE9mSXRlbXMyLCBwTGlzdCk7CiAgICByZXR1cm4gMDsKfQ==