Re: Arduino Micro Controllers
Posted: August 6th, 2019, 4:09 am
You don't need the m_count since the count is part of Array's type, therefore you also don't need the constructor Array(). The size() function can just return the template parameter count or if supported, you can make a static constexpr m_count = count and return the constexpr m_count. I'd also make the size() function constexpr if supported by the Arduino compiler as well and maybe noexcept lol.
At least this way you can use the Array class like an array:
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.
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.