Possible bug in RVO?

Yuxuan Shui via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 3 20:46:34 PDT 2016


On Monday, 4 April 2016 at 03:28:01 UTC, Yuxuan Shui wrote:
> I have encountered a weird bug.
>
> I defined a Set class, which has a opBinary!"-". And somehow 
> this:
>
> auto tmp = set_a-set_b;
>
> produces different results as this:
>
> set_a = set_a-set_b;
>
> the latter will produce an empty set.
>
> I tried to reduce the source code to get a test case. But this 
> problem just goes away after removing some code.
>
> Any ideas what I could have done wrong?

OK, I think I got a test case:

import std.traits;
struct Set {
public:
	void insert(ulong v) {
		aa[v] = true;
	}
	size_t size() const {
		return aa.length;
	}
	auto opBinary(string op)(ref Set o) const {
		Set ret;
		foreach(k; aa.byKey)
			if (k !in o.aa)
				ret.insert(k);
		return ret;
	}
	@disable this(this);
	bool[ulong] aa;
}

struct XX {
	Set a, b, tmp;
	this(int n) {
		a.insert(n);
		tmp = a-b;
		a = a-b;
	}
}
void main(){
	import std.stdio;
	XX xx = XX(1000);
	writeln(xx.a.size);
	writeln(xx.tmp.size);
}

This does not happen when 'a' is on stack, that's why I was 
having trouble reproducing it.

I don't think this is valid code, because Set has disabled 
post-blit, 'a = a-b' should report an error. However, I don't 
think current behavior of dmd is correct either.


More information about the Digitalmars-d-learn mailing list