fork download
  1. template<class input_type, class func_type>
  2. auto rfor_each(input_type&& input, func_type&& func, int) ->decltype(func(input))
  3. { return func(input);}
  4.  
  5. template<class input_type, class func_type>
  6. void rfor_each(input_type&& input, func_type&& func, ...)
  7. { for(auto&& i : input) rfor_each(i, func, 0);}
  8.  
  9. #include <iostream>
  10. int main()
  11. {
  12.  
  13. std::cout << std::endl;
  14. double B[3][3] = { { 1.2 } };
  15. rfor_each(B[1], [](double&v){v = 5;}); //iterate over doubles
  16. auto write = [](double (&i)[3]) //iterate over rows
  17. {
  18. std::cout << "{";
  19. for(double d : i)
  20. std::cout << d << ", ";
  21. std::cout << "}\n";
  22. };
  23. rfor_each(B, write );
  24. };
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
{1.2, 0, 0, }
{5, 5, 5, }
{0, 0, 0, }