Edit: added clarification for why I want this, and updated the code since I don't have any answers yet
I have a C++11 array-like class which (can be) a wrapper around a random-access iterator. Index-based access and .begin()
/.end()
can just pass through to the iterator, but there's a bit of complication when the object is const
:
template<class Size, typename DataIterator>
class Storage : public Size {
DataIterator iterator;
public:
Storage(const DataIterator &iterator, const Size &size) : Size(size), iterator(iterator) {}
auto operator[](index_t i)
-> decltype(iterator[i]) {
return iterator[i];
}
auto operator[](index_t i) const
-> MakeConst<decltype(iterator[i])> {
return iterator[i];
}
DataIterator begin() {return iterator;}
ConstWrapper<DataIterator> begin() const {return iterator;}
DataIterator end() {return iterator + this->size();}
ConstWrapper<DataIterator> end() const {return iterator + this->size();}
};
If we simply returned DataIterator
from the const
version of .begin()
and .end()
, then people holding a Storage const &
would (incorrectly!) be able to modify the array through that iterator.
Containers like std::vector
have two separate iterators (::iterator
and ::const_iterator
), but our Storage
class only has the read-write one, so we synthesise one using ConstWrapper
:
template <typename Iterator>
class ConstWrapper {
Iterator iterator;
using traits = std::iterator_traits<Iterator>;
public:
using difference_type = typename traits::difference_type;
using value_type = typename traits::value_type;
using pointer = ConstWrapper;
using reference = MakeConst<typename traits::reference>;
using iterator_category = typename traits::iterator_category;
ConstWrapper() {}
ConstWrapper(const Iterator &iterator) : iterator(iterator) {}
// The problematic cases:
auto operator[](index_t i) const
-> MakeConst<decltype(iterator[i])> {
return iterator[i];
}
auto operator*() const
-> MakeConst<decltype(*iterator)> {
return *iterator;
}
bool operator!= (const ConstWrapper& other) const {
return iterator != other.iterator;
}
/** All the other random-access iterator methods **/
};
// Specialisation to prevent infinite loops
template <typename Iterator>
class ConstWrapper<ConstWrapper<Iterator>> : ConstWrapper<Iterator> {
public:
using ConstWrapper<Iterator>::ConstWrapper;
};
The implementation forwards all the relevant methods, and has a specialisation for ConstWrapper<ConstWrapper<...>>
so that it can't wrap itself.
A key part is MakeConst
, which turns types into the correct const
variant (unlike simply adding const
, which has no effect on references):
// Converts (T & -> T const &), and (T -> const T)
using MakeConst = typename std::conditional<
std::is_reference<T>::value,
typename std::remove_reference<T>::type const &,
const T
>::type;
Does this make sense, and is it OK? Is there something else I could be doing which is more readable/efficient/etc.?
Thanks!
Size
classes were relevant.) \$\endgroup\$std::span
instead ( en.cppreference.com/w/cpp/container/span ). \$\endgroup\$