C ++ 11 provides std::array<T>
C for wrapping arrays, but only where you know the size of the array at compile time. What's the best way to handle arrays whose size is only known at runtime?
Background
I am porting some code from MSVC to GCC. MSVC provides a template stdext::checked_array_iterator<T>
to provide some protection for lines of code, for example:
std::copy(v.begin(), v.end(), stdext::checked_array_iterator<int*>(arr, numVals));
So far, I can think of two options: opt out of security checks or write my own implementation. In this post, I would be grateful for any constructive comments on this implementation:
namespace stdext {
template<typename T>
struct checked_array_iterator
{
private:
T _val;
size_t _len;
public:
typedef typename std::remove_pointer<T>::type value_type;
checked_array_iterator(T val, size_t len) : _val(val), _len(len) {}
checked_array_iterator<T> operator++(int)
{
if(_len == 0)
throw std::range_error("Array iterator overrun");
checked_array_iterator<T> retval = *this;
_val++;
_len--;
return retval;
}
checked_array_iterator<T> & operator++()
{
if(_len == 0)
throw std::range_error("Array iterator overrun");
_val++;
_len--;
return *this;
}
value_type & operator*()
{
return *_val;
}
bool operator==(checked_array_iterator<T>& other) { return other._val == _val; }
bool operator!=(checked_array_iterator<T>& other) { return !(other == *this); }
T operator->() { return _val; }
};
}
namespace std
{
template <typename T>
struct iterator_traits<stdext::checked_array_iterator<T>>
{
typedef std::ptrdiff_t difference_type;
typedef typename std::remove_pointer<T>::type value_type;
typedef T pointer;
typedef value_type& reference;
typedef std::input_iterator_tag iterator_category;
};
}
source
to share