The solution is to use a "proxy" object that will delay the actual operation:
#include <vector>
#include <iostream>
template<typename T>
struct MyArray {
std::vector<T> data;
MyArray(int size) : data(size) {}
struct Deref {
MyArray& a;
int index;
Deref(MyArray& a, int index) : a(a), index(index) {}
operator T() {
std::cout << "reading\n"; return a.data[index];
}
T& operator=(const T& other) {
std::cout << "writing\n"; return a.data[index] = other;
}
};
Deref operator[](int index) {
return Deref(*this, index);
}
};
int main(int argc, const char *argv[]) {
MyArray<int> foo(3);
foo[1] = 42;
std::cout << "Value is " << foo[1] << "\n";
return 0;
}
Simple const
-ness cannot be used because you may need to read from a non-const instance, that is the reason for which you must delay the operation: the assignment happens "after" the access and the compiler doesn't tell you if the access will be later used as a target for assignment or not.
The idea is therefore that on access you just store away the index that has been requested and wait to know if a reading or a writing operation is happening. By providing an implicit conversion operator from the proxy to T
you know when a reading operation occurs, by providing an assignment operator to the proxy from T
you know when writing occurs.