Add all std::iterator_traits required types to const_iterator of rust::Vec<T> (#157)
https://en.cppreference.com/w/cpp/iterator/iterator_traits
`std::iterator_traits` requires the following 5 types:
- `difference_type` - a signed integer type that can be used to identify distance between iterators
- `value_type` - the type of the values that can be obtained by dereferencing the iterator. This type is void for output iterators.
- `pointer` - defines a pointer to the type iterated over (value_type)
- `reference` - defines a reference to the type iterated over (value_type)
- `iterator_category` - the category of the iterator. Must be one of iterator category tags.
The current `const_iterator` for `rust::Vec<T>` only defined `value_type` and `reference` which caused an error when using `std::copy` on gcc 7.5.0 on Ubuntu but seemed to work fine on MacOS.
diff --git a/include/cxx.h b/include/cxx.h
index c02d3d9..1e33618 100644
--- a/include/cxx.h
+++ b/include/cxx.h
@@ -235,15 +235,25 @@
class const_iterator {
public:
+ using difference_type = ptrdiff_t;
using value_type = typename std::add_const<T>::type;
+ using pointer = typename std::add_pointer<
+ typename std::add_const<T>::type>::type;
using reference = typename std::add_lvalue_reference<
typename std::add_const<T>::type>::type;
+ using iterator_category = std::forward_iterator_tag;
const T &operator*() const { return *static_cast<const T *>(this->pos); }
+ const T *operator->() const { return static_cast<const T *>(this->pos); }
const_iterator &operator++() {
this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
return *this;
}
+ const_iterator operator++(int) {
+ auto ret = const_iterator(*this);
+ this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
+ return ret;
+ }
bool operator==(const const_iterator &other) const {
return this->pos == other.pos;
}