fork download
  1. #include <vector>
  2.  
  3. template <typename TopContainer, typename TopElement, typename BottomContainer>
  4. void genericFoo(const TopContainer& topList, BottomContainer TopElement::* bottomList)
  5. {
  6. for (auto it = topList.begin(); it != topList.end(); ++it) {
  7. // ...
  8. const BottomContainer& list2 = (*it).*bottomList;
  9. for (auto it2 = list2.begin(); it2 != list2.end(); ++it2) {
  10. // do some stuff
  11. }
  12. }
  13. }
  14.  
  15. struct listA {
  16. std::vector<char> listOfApples;
  17. };
  18.  
  19. struct listB {
  20. std::vector<int> listOfToys;
  21. };
  22.  
  23. std::vector<listA> listOfItems1;
  24. std::vector<listB> listOfItems2;
  25.  
  26.  
  27. int main()
  28. {
  29. std::vector<int> listB::*pList = &listB::listOfToys;
  30. genericFoo(listOfItems2, pList);
  31. return 0;
  32. }
Success #stdin #stdout 0s 2924KB
stdin
Standard input is empty
stdout
Standard output is empty