How does one suppress the automatic initialization and destruction of a type? While it is wonderful that T buffer[100]
automatically initializes all the elemen
You can create the array as array of char
s and then use placement new to create the elements when needed.
template
class Array
{
private:
char m_buffer[KCount*sizeof(T)]; // TODO make sure it's aligned correctly
T operator[](int i) {
return reinterpret_cast(m_buffer[i*sizeof(T)]);
}
After re-reading your question it seems that you want a sparse array, this sometimes goes by the name of map ;o) (of course the performance characteristics are different...)
template
class SparseArray {
std::map m_map;
public:
T& operator[](size_t i) {
if (i > KCount)
throw "out of bounds";
return m_map[i];
}