[Issue 12777] const/immutable member function violating its const-ness - confusing error message
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Wed May 21 03:28:35 PDT 2014
https://issues.dlang.org/show_bug.cgi?id=12777
--- Comment #4 from Ivan Kazmenko <gassa at mail.ru> ---
(In reply to bearophile_hugs from comment #3)
> What error message do you suggest? Perhaps something like this?
>
> test.d(3): Error: const methods (like 'S.fun') cannot modify instance
> members (like 'S.v'), only constructors can initialize them
> test.d(4): Error: immutable methods (like 'S.gun') cannot modify instance
> members (like 'S.v'), only constructors can initialize them
The part about constructors is formally false: not only constructors can
initialize a non-const instance member.
Here's a C++ variant and the respective errors for inspiration:
-----
struct S {
int v;
void fun () const {v++;}
};
class C {
int v;
void fun () const {v++;}
};
int main (void) {}
-----
GCC - g++ errors:
-----
const_member.cpp: In member function 'void S::fun() const':
const_member.cpp:3:25: error: increment of member 'S::v' in read-only object
void fun () const {v++;} ^
const_member.cpp: In member function 'void C::fun() const':
const_member.cpp:7:25: error: increment of member 'C::v' in read-only object
void fun () const {v++;}
^
-----
CLang - clang++ errors:
-----
const_member.cpp:3:25: error: read-only variable is not assignable
void fun () const {v++;}
~^
const_member.cpp:7:25: error: read-only variable is not assignable
void fun () const {v++;}
~^
-----
Visual Studio 12 - cl.exe errors:
-----
const_member.cpp(3) : error C3490: 'v' cannot be modified because it is being
accessed through a const object
const_member.cpp(7) : error C3490: 'v' cannot be modified because it is being
accessed through a const object
-----
I'd say Visual Studio's message is the most readable.
--
More information about the Digitalmars-d-bugs
mailing list