D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6362 - Can't return const reference to member
Summary: Can't return const reference to member
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 regression
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-22 03:57 UTC by Pierre LeMoine
Modified: 2011-07-22 04:25 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Pierre LeMoine 2011-07-22 03:57:48 UTC
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 kennytm 2011-07-22 04:07:10 UTC
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 Pierre LeMoine 2011-07-22 04:25:30 UTC
Ah, i see, i guess that seems right. :)