immutable/mutable aliasing

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 3 14:28:44 PDT 2014


On Thursday, 3 July 2014 at 21:06:12 UTC, Jet wrote:
> There, how to distinguish between const and immutable? thank 
> you~:)
>
> /**
> "Const types are like immutable types, except that const forms 
> a read-only view of data. Other aliases to that same data may 
> change it at any time. "
>
> "Any data referenced by the const declaration cannot be changed 
> from the const declaration, but it might be changed by other 
> references to the same data. "
> **/
>
> Can sample code, please?

When you replace 'immutable' with 'const' in your sample code,
then there is no undefined behaviour, and no cast is needed.

void foo(const int* x, int* y) {
	bar(*x); // bar(3)
	*y = 4; // perfectly fine
	bar(*x); // bar(4)
}
void main()
{
	int i = 3;
	foo(&i, &i); // no cast
}


More information about the Digitalmars-d-learn mailing list