www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4068] New: Returning references to const members yield error message

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4068

           Summary: Returning references to const members yield error
                    message
           Product: D
           Version: 2.041
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: simen.kjaras gmail.com


--- Comment #0 from Simen Kjaeraas <simen.kjaras gmail.com> 2010-04-05 05:57:30
PDT ---
The following should IMO compile:

struct S {
  const int n;
  const ref int bar( ) {
    return n;
  }
}

It fails on the line 'return n;', with the message 'Error: can only initialize
const member n inside constructor'

The same behavior exists with immutable members and references.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 05 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4068


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy yahoo.com


--- Comment #1 from Steven Schveighoffer <schveiguy yahoo.com> 2010-04-05
07:13:07 PDT ---
This probably is invalid, because the const in that position applies to the
this pointer.  It's equivalent to:

ref int bar() const {


To get the desired behavior, use parentheses:

ref const(int) bar() {

These kinds of errors are why I think allowing const at the beginning of the
function should be deprecated.

One strange thing, this also has same error:

ref const int bar() {

It would follow logically that if const applies to the function bar and not the
return value, then ref applies to the function bar as well, but it obviously
only applies to the return value.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 05 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4068


Simen Kjaeraas <simen.kjaras gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #2 from Simen Kjaeraas <simen.kjaras gmail.com> 2010-04-05 10:09:50
PDT ---
(In reply to comment #1)
 This probably is invalid, because the const in that position applies to the
 this pointer.  It's equivalent to:
 [...]
 These kinds of errors are why I think allowing const at the beginning of the
 function should be deprecated.
Indeed. Closing this one. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 05 2010