fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4.  
  5. struct Obj {
  6. int a;
  7. int b;
  8. };
  9.  
  10. template<typename ClsType, typename ValueType, size_t N>
  11. std::vector<ValueType> fn(const ClsType (&os)[N], ValueType ClsType::*member)
  12. {
  13. std::vector<ValueType> out(N);
  14. for (size_t i = 0; i < N; ++i) {
  15. out[i] = os[i].*member;
  16. }
  17. return out;
  18. }
  19.  
  20. int main() {
  21. Obj os[] { { 1, 10 }, { 2, 20 } };
  22.  
  23. auto a1 = fn(os, &Obj::a);
  24. auto a2 = fn(os, &Obj::b);
  25.  
  26. for (size_t i = 0; i < 2; ++i) {
  27. std::cout << i << ": " << a1[i] << ", " << a2[i] << '\n';
  28. }
  29. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
0: 1, 10
1: 2, 20