Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Global anonymous unions must be declared static

30 views
Skip to first unread message

Rick C. Hodgin

unread,
Oct 18, 2016, 2:51:21 PM10/18/16
to
I'm using Visual Studio 2010 for a particular application, and its VC++
compiler has the requirement that this code declared with a static
global anonymous union:

// Fails:
union {
int a1;
HWND w1;
};

// Succeeds:
static union {
int a2;
HWND w2;
};

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
LPSTR cmdLine, int nShow)
{
// Code here
}

Is it a C++ requirement that global anonymous unions be declared static,
or just Microsoft's VC++ compiler requirement? If it's a C++ requirement,
why is it a requirement? What would be the difference between a global
anonymous union and a static global anonymous union? How would the
compiler see them as different?

Best regards,
Rick C. Hodgin

David Brown

unread,
Oct 18, 2016, 3:17:18 PM10/18/16
to
It is a C++ thing - gcc gives the error "namespace-scope anonymous
aggregates must be static" for the first version.

Both versions are valid C. Both versions are utterly useless (in C and
C++), because they don't define or declare a variable, a typedef, or a
union tag.

I am not sure why C++ rejects the first version - since the union is not
named, and does not declare anything, it has no effect that I can see.
Thus I don't see why the first version is "worse" than the second one.

Ian Collins

unread,
Oct 18, 2016, 3:18:09 PM10/18/16
to
On 10/19/16 07:51 AM, Rick C. Hodgin wrote:
> I'm using Visual Studio 2010 for a particular application, and its VC++
> compiler has the requirement that this code declared with a static
> global anonymous union:
>
> // Fails:
> union {
> int a1;
> HWND w1;
> };
>
> // Succeeds:
> static union {
> int a2;
> HWND w2;
> };
>
> Is it a C++ requirement that global anonymous unions be declared static,
> or just Microsoft's VC++ compiler requirement?

C++. See section 9.5 pp5&6 of the C++11 standard.

> If it's a C++ requirement,
> why is it a requirement? What would be the difference between a global
> anonymous union and a static global anonymous union? How would the
> compiler see them as different?

As a guess I'd say it has to be static because the members are in the
scope in which the union is declared.

--
Ian

Ian Collins

unread,
Oct 18, 2016, 3:41:25 PM10/18/16
to
On 10/19/16 08:17 AM, David Brown wrote:

> Both versions are valid C. Both versions are utterly useless (in C and
> C++), because they don't define or declare a variable, a typedef, or a
> union tag.
>
> I am not sure why C++ rejects the first version - since the union is not
> named, and does not declare anything, it has no effect that I can see.
> Thus I don't see why the first version is "worse" than the second one.

It introduces its member variables into the scope in which it is
declared, so we can write:

static union
{
int a1;
int w1;
};

int main()
{
a1 = 42;
}

whether we would want to is another matter!

--
Ian
0 new messages