fork download
  1. namespace ThirdPartyLib
  2. {
  3. template <typename T>
  4. class SomeContainerFromAThirdPartyLib
  5. {
  6. public:
  7. typedef T ValueType; // not value_type!
  8. // no difference_type
  9.  
  10. class iterator
  11. {
  12. public:
  13. typedef T ValueType; // not value_type!
  14. // no difference_type
  15.  
  16. // obviously this is not how these would actually be implemented
  17. int operator != (const iterator& rhs) { return 0; }
  18. iterator& operator ++ () { return *this; }
  19. T operator * () { return T(); }
  20. };
  21.  
  22. // obviously this is not how these would actually be implemented
  23. iterator begin() { return iterator(); }
  24. iterator end() { return iterator(); }
  25. };
  26. }
  27.  
  28. #include <cstddef>
  29. #include <iterator>
  30.  
  31. namespace MyLib
  32. {
  33. template <typename T>
  34. class iterator_adapter : public T
  35. {
  36. public:
  37. // replace the following with the appropriate types for the third party iterator
  38. typedef typename T::ValueType value_type;
  39. typedef std::ptrdiff_t difference_type;
  40. typedef typename T::ValueType* pointer;
  41. typedef typename T::ValueType& reference;
  42. typedef std::input_iterator_tag iterator_category;
  43.  
  44. explicit iterator_adapter(T t) : T(t) {}
  45. };
  46.  
  47. template <typename T>
  48. class iterator_adapter<T*>
  49. {
  50. };
  51. }
  52.  
  53. #include <iostream>
  54. #include <algorithm>
  55.  
  56. using std::cout;
  57. using std::endl;
  58.  
  59. template <typename iter>
  60. typename MyLib::iterator_adapter<iter>::difference_type
  61. count(iter begin, iter end, const typename iter::ValueType& val)
  62. {
  63. cout << "[in adapter version of count]";
  64. return std::count(MyLib::iterator_adapter<iter>(begin), MyLib::iterator_adapter<iter>(end), val);
  65. }
  66.  
  67. using std::count;
  68.  
  69. int main()
  70. {
  71. char a[] = "Hello, world";
  72.  
  73. cout << "a=" << a << std::endl;
  74. cout << "count(a, a + sizeof(a), 'l')=" << count(a, a + sizeof(a), 'l') << endl;
  75.  
  76. ThirdPartyLib::SomeContainerFromAThirdPartyLib<int> container;
  77. cout << "count(container.begin(), container.end(), 0)=";
  78. cout << count(container.begin(), container.end(), 0) << endl;
  79.  
  80. return 0;
  81. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
a=Hello, world
count(a, a + sizeof(a), 'l')=3
count(container.begin(), container.end(), 0)=[in adapter version of count]0