const, auto and struct/class methods

Andrea Fontana nospam at example.com
Mon Nov 25 01:13:36 PST 2013


On Monday, 25 November 2013 at 09:05:39 UTC, Joseph Rushton 
Wakeling wrote:
> 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

auto bar() { return cast(const int) 10; }

writeln(typeid(bar()));

?


More information about the Digitalmars-d-learn mailing list