Destructors, const structs, and opEquals

Michel Fortin michel.fortin at michelf.com
Sat Dec 11 09:19:09 PST 2010


On 2010-12-11 11:48:36 -0500, spir <denis.spir at gmail.com> said:

> If parameters are 'in' or 'const' by default, then whether they are passed
> by value or by ref has no consequence, I guess. The compiler can then safel
> y choose the most efficent more --what it can do as it knows sizeof-- gross
> ly structs by ref, the rest by value. Is this reasoning correct?

No it can't because of aliasing (the object might be referenced by 
something else). Look at this simple example:

	struct A { int value; }

	bool test(const A a1, ref A a2) {
		++a2.value;
		return a1.value == a2.value;
	}

	void main() {
		A a;
		a.value = 8;
		bool result = test(a, a);
		assert(result == false);
		assert(a.value == 9);
	}

If "a1" is passed by ref under the hood, it can only be done whenever 
you know for sure there is no aliasing, or if the data is "immutable", 
otherwise you'll have side effects. So while it could be done in some 
circumstances (strongly pure functions and immutable parameters), 
there's still an important need for a more general solution.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list