www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - function without "this" cannot be const?

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
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?! :-(

-- 
Shriramana Sharma, Penguin #395953
Dec 20 2015
next sibling parent Jakob Ovrum <jakobovrum gmail.com> writes:
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?! :-(
Change it to: extern(C) const(char)* textAttrN(const char * specString, size_t n); ^ ^
Dec 20 2015
prev sibling parent reply Basile B. <b2.temp gmx.com> writes:
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.
Dec 20 2015
parent Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
Basile B.  wrote:

 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.
Thank you people. -- Shriramana Sharma, Penguin #395953
Dec 20 2015