const violation?

H. S. Teoh hsteoh at
Fri Jan 13 12:27:56 PST 2012

Why does the following code compile?

	import std.stdio;
	int f(ref int x) {
		return x++;
	class A {
		int x=123;
		int g() const {
			return f(x);
	void main() {
		auto a = new A;

Shouldn't the const member g() be prohibited from passing a ref to a
member variable to f()?

But this code not only compiles, it outputs:


So I've managed to call a const member of A to alter the value of A.x?
Am I misunderstanding the meaning of const when applied to a member
function, or is this a compiler bug?

P.S. I'm using gdc-4.6; does dmd also have this behaviour?


Lawyer: (n.) An innocence-vending machine, the effectiveness of which
depends on how much money is inserted.

More information about the Digitalmars-d-learn mailing list