fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <type_traits>
  5. #include <iterator>
  6.  
  7. // Figure out if contiguous here --> template function bool is_contiguous<Type>();
  8.  
  9. // User-defined iterator
  10. using Custom_Iterator = int*;
  11.  
  12. template<typename Iterator>
  13. constexpr auto is_contiguous()
  14. -> typename std::enable_if<std::is_same<Iterator,Custom_Iterator>::value, bool>::type
  15. {
  16. return true; // change to false if not actually contiguous
  17. }
  18.  
  19.  
  20. // Simple container
  21. template<typename Iterator>
  22. constexpr auto is_contiguous()
  23. -> typename std::enable_if<std::is_same<Iterator,std::string::iterator>::value, bool>::type
  24. {
  25. return true;
  26. }
  27.  
  28.  
  29. // STL Containers (seems to require typename = void voodoo)
  30. template<typename Iterator, typename U = typename std::vector<typename std::iterator_traits<Iterator>::value_type>::iterator>
  31. constexpr auto is_contiguous()
  32. -> typename std::enable_if<std::is_same<Iterator,U>::value, bool>::type
  33. {
  34. return true;
  35. }
  36.  
  37. template<typename Iterator, typename U = typename std::list<typename std::iterator_traits<Iterator>::value_type>::iterator, typename = void>
  38. constexpr auto is_contiguous()
  39. -> typename std::enable_if<std::is_same<Iterator,U>::value, bool>::type
  40. {
  41. return false;
  42. }
  43.  
  44.  
  45.  
  46. // Do work here
  47. template<typename Iterator>
  48. auto f(Iterator beg, Iterator end)
  49. -> typename std::enable_if< ! is_contiguous<Iterator>(), void>::type
  50. {
  51. std::cout << "Not contiguous:";
  52. for(auto it = beg; it != end; it = std::next(it))
  53. {
  54. std::cout << " " << *it;
  55. }
  56. std::cout << std::endl;
  57. }
  58.  
  59. template<typename Iterator>
  60. auto f(Iterator beg, Iterator end)
  61. -> typename std::enable_if<is_contiguous<Iterator>(), void>::type
  62. {
  63. std::cout << "Contiguous:";
  64. for(auto it = beg; it != end; it = std::next(it))
  65. {
  66. std::cout << " " << *it;
  67. }
  68. std::cout << std::endl;
  69. }
  70.  
  71.  
  72. int main()
  73. {
  74. std::cout << "Custom Data Set --> ";
  75. int data[] = {1,2,3};
  76. Custom_Iterator begin = data;
  77. Custom_Iterator end = data+3;
  78. f(begin, end);
  79.  
  80. std::list<int> x = {1,2,3};
  81. std::cout << "List --> ";
  82. f(x.begin(), x.end());
  83.  
  84. std::cout << "Vector<int> --> ";
  85. std::vector<int> v = {1,2,3};
  86. f(v.begin(), v.end());
  87.  
  88. std::cout << "Vector<double> --> ";
  89. std::vector<double> vd = {1.1,2.2,3.3};
  90. f(vd.begin(), vd.end());
  91.  
  92. std::cout << "String --> ";
  93. std::string s = "WTF";
  94. f(s.begin(), s.end());
  95. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Custom Data Set --> Contiguous: 1 2 3
List --> Not contiguous: 1 2 3
Vector<int> --> Contiguous: 1 2 3
Vector<double> --> Contiguous: 1.1 2.2 3.3
String --> Contiguous: W T F