const method and return type

Adam D. Ruppe destructionator at gmail.com
Fri Dec 13 16:30:07 PST 2013


On Saturday, 14 December 2013 at 00:09:03 UTC, Andrea Fontana 
wrote:
> I'm not changing anything. Just returning it.

Right, it is just that "auto" keeps the exact type, and in the 
const method, the exact type of the members are all const too 
since that's how the language enforces that you don't change 
anything.

You are allowed implicitly cast const(int) to int, which is why 
the version that specifically returns int works, it is just that 
auto doesn't do any implicit conversions.

The same thing happens if you do

const(int) a = 10;
auto foo = a;
foo = 0; // test500.d(5): Error: cannot modify const expression 
foo


The auto var decl there keeps const too. There's some discussion 
that auto might strip off the top level const where it is 
otherwise allowed, but I'm not sure if that's actually going to 
happen or not.


More information about the Digitalmars-d-learn mailing list