fork(12) 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< is_iterable<T>::value >::type
  44. for_all_ele(T&& arr,_F&& fn)
  45. {
  46. for(auto &x:arr)
  47. for_all_ele(x,fn);
  48.  
  49. }
  50.  
  51. template <typename T,typename _F>
  52. inline typename std::enable_if< not is_iterable<T>::value >::type
  53. for_all_ele(T&& x,_F&& fn)
  54. {
  55. fn(x);
  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.  
Compilation error #stdin compilation error #stdout 0s 3456KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of 'typename std::enable_if<decltype (is_iterable_impl<T>(0))::value>::type test::for_all_ele(T&&, _F&&) [with T = int (&)[5]; _F = main()::<lambda(int&)>&; typename std::enable_if<decltype (is_iterable_impl<T>(0))::value>::type = void]':
prog.cpp:47:14:   required from 'typename std::enable_if<decltype (is_iterable_impl<T>(0))::value>::type test::for_all_ele(T&&, _F&&) [with T = int (&)[2][5]; _F = main()::<lambda(int&)>; typename std::enable_if<decltype (is_iterable_impl<T>(0))::value>::type = void]'
prog.cpp:75:47:   required from here
prog.cpp:47:14: error: no matching function for call to 'for_all_ele(int&, main()::<lambda(int&)>&)'
   for_all_ele(x,fn);
              ^
prog.cpp:44:1: note: candidate: template<class T, class _F> typename std::enable_if<decltype (is_iterable_impl<T>(0))::value>::type test::for_all_ele(T&&, _F&&)
 for_all_ele(T&& arr,_F&& fn)
 ^
prog.cpp:44:1: note:   template argument deduction/substitution failed:
prog.cpp: In substitution of 'template<class T, class _F> typename std::enable_if<decltype (is_iterable_impl<T>(0))::value>::type test::for_all_ele(T&&, _F&&) [with T = int&; _F = main()::<lambda(int&)>&]':
prog.cpp:47:14:   recursively required from 'typename std::enable_if<decltype (is_iterable_impl<T>(0))::value>::type test::for_all_ele(T&&, _F&&) [with T = int (&)[5]; _F = main()::<lambda(int&)>&; typename std::enable_if<decltype (is_iterable_impl<T>(0))::value>::type = void]'
prog.cpp:47:14:   required from 'typename std::enable_if<decltype (is_iterable_impl<T>(0))::value>::type test::for_all_ele(T&&, _F&&) [with T = int (&)[2][5]; _F = main()::<lambda(int&)>; typename std::enable_if<decltype (is_iterable_impl<T>(0))::value>::type = void]'
prog.cpp:75:47:   required from here
prog.cpp:44:1: error: no type named 'type' in 'struct std::enable_if<false, void>'
stdout
Standard output is empty