www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unions and Structs

reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
So this is an error?

union flob {
	ulong data;
	struct thingy {
		uint data;
		uint bits;
	}
	thingy burble;
};

because you cannot have a union field with a name that is also the name
of a struct field defined within the union.
=C2=A0=C2=A0
--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
Jan 18
parent reply anonymous <anonymous example.com> writes:
On 18.01.2016 18:10, Russel Winder via Digitalmars-d-learn wrote:
 So this is an error?

 union flob {
 	ulong data;
 	struct thingy {
 		uint data;
 		uint bits;
 	}
 	thingy burble;
 };

 because you cannot have a union field with a name that is also the name
 of a struct field defined within the union.
I don't see the problem. You have to access the thingy's 'data' through the 'burble' member, so there is no ambiguity, is there? This would be different, and dmd rejects it accordingly: ---- union flob { ulong data; struct { uint data; /* test.d(4): Error: variable test.flob.data conflicts with variable test.flob.data at test.d(2) */ uint bits; } } ----
Jan 18
parent reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 2016-01-18 at 18:17 +0100, anonymous via Digitalmars-d-learn
wrote:
 On 18.01.2016 18:10, Russel Winder via Digitalmars-d-learn wrote:
 So this is an error?
=20
 union flob {
 	ulong data;
 	struct thingy {
 		uint data;
 		uint bits;
 	}
 	thingy burble;
 };
=20
 because you cannot have a union field with a name that is also the
 name
 of a struct field defined within the union.
=20 I don't see the problem. You have to access the thingy's 'data' through=C2=A0 the 'burble' member, so there is no ambiguity, is there?
That is what I thought. There is a bit of this stuff in the real more complicated case. But it transpires I was looking at the wrong bits=E2=80= =A6
 This would be different, and dmd rejects it accordingly:
 ----
 union flob {
 	ulong data;
 	struct {
 		uint data; /* test.d(4): Error: variable test.flob.data
 conflicts with=C2=A0
 variable test.flob.data at test.d(2) */
 		uint bits;
 	}
 }
Ah, this is actually what is there and what the problem is. It seems DStep is producing somewhat strange D from complicated C unions. The problem is real as you point out in the generated code, but the generated code is nothing like what I would have generated by hand, which wouldn't have the problem. I shall go away and procrastinate for some, to be decided, time about whether to continue with D or revert back to C++. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Jan 18
parent reply Jacob Carlborg <doob me.com> writes:
On 2016-01-18 19:11, Russel Winder via Digitalmars-d-learn wrote:

 It seems DStep is producing somewhat strange D from complicated C
 unions.
Please report any issues to [1] with a test case. C code, expected D code, actual D code. [1] https://github.com/jacob-carlborg/dstep/issues -- /Jacob Carlborg
Jan 18
parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 2016-01-18 at 21:50 +0100, Jacob Carlborg via Digitalmars-d-
learn wrote:
 On 2016-01-18 19:11, Russel Winder via Digitalmars-d-learn wrote:
=20
 It seems DStep is producing somewhat strange D from complicated C
 unions.
=20 Please report any issues to [1] with a test case. C code, expected D=C2=
=A0
 code, actual D code.
=20
 [1] https://github.com/jacob-carlborg/dstep/issues
I made a start, but they aren't full reports as yet. https://github.com/jacob-carlborg/dstep/issues/46 https://github.com/jacob-carlborg/dstep/issues/47 --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Jan 19