Topic: Why are static data members disallowed in unions?
Author: scott douglass <sdouglass@arm.com>
Date: 2000/10/05 Raw View
Hello,
In 9.5/1 the standard says:
>>
If a union contains a static data member, or a member of reference type,=20
the pro=ADgram
is ill-formed.
<<
Why are static data members disallowed in unions? They've been disallowed=
=20
at least since the ARM. They would make reasonable sense to me. I'd use=
=20
them for the same reasons that I use static data members in a class.
union U {
U() : i(0) { ++n; recent =3D this; }
~U() { --n; if (recent =3D=3D this) recent =3D 0; }
static int num() { return n; }
int i;
static int n;
static U* recent;
};
int U::n;
U* U::recent;
I guess there could be squeamishness about the fact that union's non-stat=
ic=20
data members are overlapped and the static data members would be=20
distinct. But this doesn't seem difficult to grasp to me (and if you wan=
t=20
the static data members overlapped you could put them in an union).
Instead I have to do:
int U_n;
union U;
U* U_recent;
union U {
U() : i(0) { ++U_n; U_recent =3D this; }
~U() { --U_n; if (U_recent =3D=3D this) U_recent =3D 0; }
static int num() { return U_n; }
int i;
};
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]