Should the "front" range primitive be "const" ?

Drone1h drone1h at gmail.com
Tue Jan 30 01:05:54 UTC 2018


Hello all,



I am trying to implement a ("struct template" ? what is the 
correct word ?) range that just forwards its primitives ("empty", 
"front", "popFront") to another range, possibly with some very 
limited filtering/alteration, as std.range.Take (just to learn).

Initially, the "front" member function (property) used to be 
declared "const", but that was not accepted when the underlying 
range (denoted "R" in the code below) was std.stdio.File.ByChunk 
("Error: mutable method std.stdio.File.ByChunk.front is not 
callable using a const object").

Is there any value in having the "front" range primitive declared 
to be a "const"  member function ?

And if so, is the following implementation okay ? Could it be 
further simplified ?

     struct Taker (R)
     {
         private R _r;

         ...

         static if (functionAttributes ! (R.front) & 
FunctionAttribute.const_)
             public @property auto front () const { return 
_r.front; }
         else
             public @property auto front ()       { return 
_r.front; }

         ...
     }



Thank you respectfully !
Drone1h



More information about the Digitalmars-d-learn mailing list