What guarantees does D 'const' provide, compared to C++?

Mehrdad wfunction at hotmail.com
Thu Aug 16 19:01:09 PDT 2012


On Friday, 17 August 2012 at 01:50:02 UTC, Chris Cain wrote:
> On Friday, 17 August 2012 at 01:43:03 UTC, Mehrdad wrote:
>> Isn't that kinda useless, if it tells you nothing about the 
>> object itself?
>
> Not sure what your point is. It tells you enough about how you 
> work with that "object itself"


Are you sure?



struct MyStruct
{
	static int* x;
	int y;

	this() { }
	this(int* z) { x = z; }

	auto getValue() const
	{
		++*x;
		return this.y;
	}
}

auto s = MyStruct();
s = MyStruct(&s.y);

s.getValue();  // const, but returns 1
s.getValue();  // const, but returns 2
s.getValue();  // const, but returns 3



So, effectively, the mere const-ness (and even its transitivity) 
is useless to the compiler.



> I'll let Mr. Davis confirm which he was talking about. The only 
> thing that's clear is that our understandings of his point 
> differ.

Okay sure, thanks for trying to explain it anyway!


More information about the Digitalmars-d mailing list