Example code that fails to compile with DMD 2.093.0 on Linux 64-bit:
import std.range;
class C { }
struct A {
private:
SortedRange!(C[]) _backend;
public:
inout(C) front() inout {
return _backend.front();
}
bool empty() const {
return _backend.empty();
}
}
void main() { }
This generates two errors: Can't call front() on an inout _backend, and can't call empty() on const _backend. The compiler even suggests annotating SortedRange's methods with const/inout.
Expected instead: The above code compiles.
Workaround: Replace "SortedRange!(C[]) _backend;" with "C[] _backend;", and design struct A to ensure that _backend is always sorted.
In Phobos's source for std.range.SortedRange, empty() is declared non-const, but has a comment "//const". This has been untouched since at least 2014.
---
Related bug:
https://issues.dlang.org/show_bug.cgi?id=9792
length field of a const SortedRange
Comment #1 by s.naarmann — 2020-09-08T15:34:37Z
Discussion on the D Forums:
https://forum.dlang.org/thread/[email protected]
Gist: Instead of adding const/inout, which might not fit every wrapped range, we should use `template this` to let the compiler figure out the appropriate annotations for empty/front/... individually for each wrapped range.
Comment #2 by robert.schadek — 2024-12-01T16:37:33Z