D: Why is opIndex not const-qualified in the std.container.Array class? -


i wanted make use of std.container.array , proceeded create class getter member function returns value array class. realised not able const-qualify getter, since opindex mutable function.

i tried changing source code const-qualify array.opindex, , built fine. however, unit tests in std.algorithm did not pass, complaining return value of array.opindex not lvalue.

here code array.opindex:

ref t opindex(size_t i) {     version (assert) if (!_data.refcountedstore.isinitialized) throw new rangeerror();     return _data._payload[i]; } 

is there i'm missing here? why not const-qualified?

there number of issues making containers const-correct, since const makes can't change anything in internals, unlike in c++, make stuff mutable long made sure functions logically const. iirc, there operations array theoretically const can't due how of internals work. , wouldn't surprise me if because of that, folks have worked on didn't make of const, if of be.

as opindex, don't see obvious in implementation couldn't const, , fact compiled @ implies might work. however, if that, need overload rather make particular overload const, or won't able assign - presumably std.algorithm complaining it. so, you'd need like

ref t opindex(size_t i) {...} ref const(t) opindex(size_t i) const {...} 

so still works assign - e.g. arr[5] = "foo"; - long array isn't const. however, since many of array's operations can't const due how implementation works, don't know how useful make functions opindex const, because you'll limited in can const array!t if every member function can const const.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -