D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4976 - Can't use auto on const member functions.
Summary: Can't use auto on const member functions.
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: spec
Depends on:
Blocks:
 
Reported: 2010-10-02 17:26 UTC by Peter Alexander
Modified: 2012-01-02 02:25 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Peter Alexander 2010-10-02 17:26:27 UTC
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 Simen Kjaeraas 2010-10-02 17:40:01 UTC
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 Peter Alexander 2010-10-02 17:51:12 UTC
Oh, I would have never expected that.

Thanks.
Comment 3 Stewart Gordon 2010-10-02 18:43:55 UTC
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.