I recently wanted to make use of std.container.Array and proceeded to create a class with a getter member function which returns a value from the Array class. I quickly realised that I was not able to const-qualify my getter, since opIndex is a mutable function.
I tried changing the source code to const-qualify Array.opIndex, and it built fine. However, some unit tests in std.algorithm did not pass, complaining that the return value of Array.opIndex is not an lvalue.
Here is the code for Array.opIndex:
ref T opIndex(size_t i)
{
version (assert) if (!_data.refCountedStore.isInitialized) throw new RangeError();
return _data._payload[i];
}
Is there something I'm missing here? Why is it not const-qualified?