Bug 6362 – Can't return const reference to member

Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-07-22T03:57:00Z
Last change time
2011-07-22T04:25:30Z
Assigned to
nobody
Creator
yarrluben+dbugs

Comments

Comment #0 by yarrluben+dbugs — 2011-07-22T03:57:48Z
The problem started with dmd 2.054. I've had no problems with this in earlier versions. struct foo { public: const ref int get() const { return bar[0]; } private: int bar[1]; } void main(){ auto a = foo(); } ==> Error: cast(int)this.bar[0u] is not an lvalue
Comment #1 by kennytm — 2011-07-22T04:07:10Z
I believe the previous versions are accept-invalid. The leading 'const' is equivalent to the trailing 'const', which is applying the 'const' to 'this' only. Therefore, the function's type is in fact ref int get() const; If you want to return a const int, apply it directly on the return type. ------------------ struct foo { public: ref const(int) get() const { return bar[0]; } // ^^^^^^^^^^ private: int bar[1]; } void main(){ auto a = foo(); } ------------------
Comment #2 by yarrluben+dbugs — 2011-07-22T04:25:30Z
Ah, i see, i guess that seems right. :)