const, auto and struct/class methods

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Mon Nov 25 01:05:33 PST 2013


Hello all,

If I mark a struct or class method as const, this is assumed to apply to the 
entire method, i.e. that nothing in it will modify any internal data of the 
struct/class.

     struct Foo
     {
         const auto bar()
         {
             // I can't modify any of the
             // internal data of Foo here
         }
     }

Suppose instead that I want a method that _may_ modify internal data, but will 
return an entity that is itself const.  Is there any way to do this while having 
the return type being declared via auto?

i.e. what I want to be able to do is something like,

      struct Foo
      {
           const(auto) bar()
           {
               // modifies internal data of Foo
               // but returns a const type
           }
      }

(Note that const(auto) of course is not valid D, I use it for illustrative 
purposes:-)

Is something like this possible, and if so, what's the correct notation?

Thanks and best wishes,

     -- Joe


More information about the Digitalmars-d-learn mailing list