I need to implement a safe array class that controls index when accessing underlying C array:
template <typename T, int N> class SafeArray
{
public:
T &operator[](int index)
{
assert(0 <= index && index < N);
return m_data[index];
}
private:
T m_data[N];
};
And instead of bool a[3];, now I write SafeArray<bool, 3> a;.
How do I support array initialization like bool b[3] = {false};? I mean what should I do to get b.m_data[] = {false, false, false} after SafeArray<bool, 3> b; has been constructed?
I guess I should add a constructor to SafeArray, but what would be a body of that constructor? Template parameter T can be anything, not necessarily bool. I am using pre-C++11.