Code: Select all
template<typename T, unsigned int count>
class Array
{
static constexpr unsigned int m_count = count;
public:
// convenience functions
constexpr T& operator[]( unsigned int _index )noexcept{
assert( _index < m_count && "Index out of range" );
return elements[ _index ];
}
constexpr T const& operator[]( unsigned int _index )const noexcept{
assert( _index < m_count && "Index out of range" );
return elements[ _index ];
}
constexpr unsigned int size()const noexcept {return m_count;}
T elements[ count ];
};
Array<int, 10> ints_A;
Array<int, 10> ints_B;
Old way: ints_A.elements[ 0 ] = 3;
New way: ints_B[ 0 ] = 3;
Plus, this way you get some debugging help if you try accessing out of bounds.
Now you just need to make an ArrayIterator, ArrayConstIterator, Array::begin() and Array::end() so you can use range based for loops.