fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. #include <type_traits>
  5. #include <bits/range_access.h>
  6.  
  7. /* comment the following line and everything is fine... */
  8. #define nsp
  9.  
  10. #ifdef nsp
  11. namespace test
  12. {
  13. #endif
  14.  
  15.  
  16. namespace detail
  17. {
  18. // To allow ADL with custom begin/end
  19. using std::begin;
  20. using std::end;
  21.  
  22. template <typename T>
  23. auto is_iterable_impl(int)
  24. -> decltype (
  25. begin(std::declval<T&>()) != end(std::declval<T&>()), // begin/end and operator !=
  26. ++std::declval<decltype(begin(std::declval<T&>()))&>(), // operator ++
  27. *begin(std::declval<T&>()), // operator*
  28. std::true_type {});
  29.  
  30. template <typename T>
  31. std::false_type is_iterable_impl(...);
  32.  
  33. }
  34.  
  35. template <typename T>
  36. using is_iterable = decltype(detail::is_iterable_impl<T>(0));
  37.  
  38.  
  39.  
  40.  
  41.  
  42. template <typename T,typename _F>
  43. inline typename std::enable_if< not is_iterable<T>::value >::type
  44. for_all_ele(T&& x,_F&& fn)
  45. {
  46. fn(x);
  47. }
  48.  
  49.  
  50. template <typename T,typename _F>
  51. inline typename std::enable_if< is_iterable<T>::value >::type
  52. for_all_ele(T&& arr,_F&& fn)
  53. {
  54. for(auto &x:arr)
  55. for_all_ele(x,fn);
  56.  
  57. }
  58.  
  59.  
  60. #ifdef nsp
  61. }
  62. using namespace test;
  63. #endif
  64.  
  65.  
  66.  
  67.  
  68. #include <iostream>
  69. using namespace std;
  70.  
  71. int main()
  72. {
  73. int x[2][5]= {1,2,5,68,9,12,35,6,32,89};
  74.  
  75. for_all_ele(x,[](int &xij){ cout<<xij<<","; });
  76.  
  77. return 0;
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
1,2,5,68,9,12,35,6,32,89,