function without "this" cannot be const?

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 20 21:05:50 PST 2015


On Monday, 21 December 2015 at 02:03:14 UTC, Shriramana Sharma 
wrote:
> I'm trying to interface to a C function:
>
> extern(C) const char * textAttrN(const char * specString, 
> size_t n);
>
> and getting the error:
>
> Error: function <src>.textAttrN without 'this' cannot be const
>
> Please advise as to what I'm doing wrong?! :-(

without the parens, 'const' means that the function doesn't 
mutate the state of the object or of the struct it's declared in. 
So it's meaningless for a global function.

To avoid the confusion, take the habit to put 'const' at the 
right of the function declaration when it's related to a member 
function and only to the left when it's related to the type, and 
then to the left <=> always with parens.


More information about the Digitalmars-d-learn mailing list