Guest User

Untitled

a guest
Nov 25th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. template<typename I>
  2. class Foo
  3. {
  4. const I Begin ;
  5. const I End ;
  6. I Position;
  7.  
  8. public:
  9. bool append(const I begin, const I end)
  10. {
  11. if(std::distance(begin, end) <= std::distance(Position, End))
  12. {
  13. Position = std::copy(begin,end,Position);
  14. return true;
  15. }
  16. return false;
  17. }
  18. };
  19.  
  20. auto foo = Foo<int *>(/* как-то инициализируем */);
  21. const int data[] = {0,1,2,3,4,5,6,7};
  22. foo.append( std::begin(data)
  23. , std::end (data) );
  24.  
  25. template<typename U>
  26. bool append(U begin, U end)
  27. {
  28. //... здесь так же
  29. }
  30.  
  31. template<typename U>
  32. bool append(U begin, U end)
  33. {
  34. //... здесь так же
  35. }
  36.  
  37. using T = typename std::add_pointer<
  38. typename std::add_const<
  39. typename std::remove_pointer<I>::type >
  40. ::type >::type;
  41.  
  42. bool append(T begin, T end)
  43. ...
  44.  
  45. #include <type_traits>
  46. #include <iterator>
  47.  
  48. template<class T>
  49. struct is_const_iterator
  50. : std::is_const<std::iterator_traits<T>::reference>
  51. {};
  52.  
  53. // для интераторов STL
  54. template<class T>
  55. struct iterator_traits_ex: std::iterator_traits<T>
  56. {
  57. typedef T original_iterator;
  58. //typedef /* не известно */ iterator;
  59. //typedef /* не известно */ const_iterator;
  60. };
  61.  
  62. template<class T>
  63. struct iterator_traits_ex<T *>: std::iterator_traits<T*>
  64. {
  65. typedef T * original_iterator;
  66. typedef T * iterator;
  67. typedef const T * const_iterator;
  68. };
  69.  
  70. template<class T>
  71. struct iterator_traits_ex<const T *>: std::iterator_traits<const T *>
  72. {
  73. typedef const T * original_iterator;
  74. typedef T * iterator;
  75. typedef const T * const_iterator;
  76. };
  77.  
  78. template<typename I>
  79. class Foo
  80. {
  81. static_assert(!is_const_iterator<I>::value, "Type of I can't be a const iterator!");
  82.  
  83. // ...
  84.  
  85. typedef typename iterator_traits_ex<I>::const_iterator ConstIterator;
  86. public:
  87. bool append(ConstIterator begin, ConstIterator end)
  88. {
  89. // ...
  90. }
  91. };
Add Comment
Please, Sign In to add comment