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

Mehrdad wfunction at hotmail.com
Thu Aug 16 19:30:42 PDT 2012


On Friday, 17 August 2012 at 02:25:22 UTC, Jonathan M Davis wrote:
> Yeah. It's more than C++, but it's still pretty limited without 
> pure, and if even with pure, the optimizations can still be 
> pretty limited.

Yeah, I'm only worried about the language here, not the 
implementation.



On Friday, 17 August 2012 at 02:14:22 UTC, Jonathan M Davis wrote:
> What I meant is that you know that nothing was altered through 
> the reference that the getter returned. You don't have any such 
> guarantee in C++.


I'm not following..
It seems to contradict my second example:


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 unless you're expecting the compiler to have the 
implementation for the entire class available in order for it to 
be able to do any kind of optimization (in which case, it would 
have to do a whole bunch of inference to figure out the aliasing 
issues, which would amount to what a C++ could try do just as 
well), I'm not seeing where the additional guarantees/potential 
optimizations are.


More information about the Digitalmars-d mailing list