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