Bug 4976 – Can't use auto on const member functions.

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-10-02T17:26:00Z
Last change time
2012-01-02T02:25:47Z
Keywords
spec
Assigned to
nobody
Creator
peter.alexander.au

Comments

Comment #0 by peter.alexander.au — 2010-10-02T17:26:27Z
When you try to use type inference on a const member function, you get a compiler error: class Foo { public: auto foo() const { return 0; } } Compiling gives: test.d(4): no identifier for declarator foo test.d(4): semicolon expected, not 'const' test.d(4): Declaration expected, not 'return' Removing the 'const' allows it to compile, but the function can no longer be called on const objects. The same applies for structs and @property methods. This is using DMD 2.049.
Comment #1 by simen.kjaras — 2010-10-02T17:40:01Z
This is because const and auto are both storage classes. What you want is easily achieved by using this syntax: class Foo { public: const foo( ) { return 0; } }
Comment #2 by peter.alexander.au — 2010-10-02T17:51:12Z
Oh, I would have never expected that. Thanks.
Comment #3 by smjg — 2010-10-02T18:43:55Z
But given its counter-intuitiveness, surely this is just a quirk of the grammar, and not a deliberate design feature? I suppose this is really part of the quirk whereby the grammar handles auto as an attribute rather than as what it should be: a placeholder for a type.
Comment #4 by k.hara.pg — 2012-01-02T02:25:47Z