I tried to put an anonymous struct inside an anonymous union.
Unfortunately, this does not work. With the complete code example
below, I get the compiler error: error C2039: 'f1' : is not a member
of 'dataElement'

I can't think of any other way to define this type of structure.
Basically, I want to be able to access a 32-bit integer in multiple
ways, either as a full integer or broken out into bit fields, using
the simplest notation possible. Actually, I'd much prefer not using
an anonymous struct because these are Microsoft-specific. Anyone have
any ideas?

For the example below, the simplest notation would be:

deTemp.f1
deTemp.f2
deTemp.dwAllData

// ---------------------------- BEGIN CODE

#include "stdafx.h"
using namespace std;

struct t_fields {
unsigned __int16 f1 : 16;
unsigned __int16 f2 : 16;
};


class dataElement {
public:
union { // anonymous union
unsigned __int32 dwAllData;
struct t_fields; // anonymous struct
};
};


int _tmain(int argc, _TCHAR* argv[])
{
class dataElement deTemp;
cout << "size is: " << sizeof(class dataElement) << "\n";
deTemp.f1 = 1000;

return 0;
}

// ---------------------------- END CODE

Re: anonymous structs and unions by Vincent

Vincent
Sun May 09 15:57:19 CDT 2004

According to the docs, anonymous structures "are a Microsoft C extension"
and "C++ does not allow anonymous structures".

On Sun, 09 May 2004 15:39:00 -0400, Shailesh Humbad <humbads1@hotmail.com>
wrote:

>I tried to put an anonymous struct inside an anonymous union.
>Unfortunately, this does not work. With the complete code example
>below, I get the compiler error: error C2039: 'f1' : is not a member
>of 'dataElement'
>
>I can't think of any other way to define this type of structure.
>Basically, I want to be able to access a 32-bit integer in multiple
>ways, either as a full integer or broken out into bit fields, using
>the simplest notation possible. Actually, I'd much prefer not using
>an anonymous struct because these are Microsoft-specific. Anyone have
>any ideas?
>
>For the example below, the simplest notation would be:
>
>deTemp.f1
>deTemp.f2
>deTemp.dwAllData
>
>// ---------------------------- BEGIN CODE
>
>#include "stdafx.h"
>using namespace std;
>
>struct t_fields {
> unsigned __int16 f1 : 16;
> unsigned __int16 f2 : 16;
>};
>
>
>class dataElement {
>public:
> union { // anonymous union
> unsigned __int32 dwAllData;
> struct t_fields; // anonymous struct
> };
>};
>
>
>int _tmain(int argc, _TCHAR* argv[])
>{
> class dataElement deTemp;
> cout << "size is: " << sizeof(class dataElement) << "\n";
> deTemp.f1 = 1000;
>
> return 0;
>}
>
>// ---------------------------- END CODE

--
- Vince

Re: anonymous structs and unions by Steve

Steve
Sun May 09 16:08:35 CDT 2004

Will this way work for you?
(Seems to compile for me)

#include "stdafx.h"

class dataElement
{
public:
union
{ // anonymous union
unsigned __int32 dwAllData;
struct // anonymous struct
{
unsigned __int16 f1 : 16;
unsigned __int16 f2 : 16;
};
};
};


int main(int argc, char* argv[])
{
class dataElement deTemp;
deTemp.f1 = 1000;

return 0;
}



Re: anonymous structs and unions by Shailesh

Shailesh
Sun May 09 17:21:15 CDT 2004

Oh, I misunderstood their statement. I thought it meant anonymous
structures were available since Microsoft C came out, so even in
Microsoft C++, despite the fact that ANSI C++ does not allow them.

That's what happens with pithy MSDN Library statements...

Vincent Fatica wrote:

> According to the docs, anonymous structures "are a Microsoft C extension"
> and "C++ does not allow anonymous structures".
>

Re: anonymous structs and unions by Shailesh

Shailesh
Sun May 09 17:29:31 CDT 2004

Yes, removing the "t_fields" struct name worked for me too. I tried
it in gcc, and it works too (changing __int32 to int and __int16 to
short). So what's the story with these anonymous structs? Or is this
not an anonymous struct? ANSI C++ disallows them but Microsoft C++ and
gcc support them?

Steve Fallows wrote:

> Will this way work for you?
> (Seems to compile for me)
>
> #include "stdafx.h"
>
> class dataElement
> {
> public:
> union
> { // anonymous union
> unsigned __int32 dwAllData;
> struct // anonymous struct
> {
> unsigned __int16 f1 : 16;
> unsigned __int16 f2 : 16;
> };
> };
> };
>
>
> int main(int argc, char* argv[])
> {
> class dataElement deTemp;
> deTemp.f1 = 1000;
>
> return 0;
> }
>
>

Re: anonymous structs and unions by John

John
Sun May 09 18:49:36 CDT 2004

"Vincent Fatica" <abuse@localhost> wrote in message
news:409e9b2f$1@localhost
> According to the docs, anonymous structures "are a Microsoft C
> extension" and "C++ does not allow anonymous structures".


Standard C++ doesn't support them, but VC++ does. It has to because
windows.h (and the files it #includes) is full of them.


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


Re: anonymous structs and unions by v-garych

v-garych
Mon May 10 21:21:48 CDT 2004

Hi Shailesh,

> So what's the story with these anonymous structs? Or is this
> not an anonymous struct? ANSI C++ disallows them but Microsoft C++ and
> gcc support them?

Based on the following MSDN doc, the anonymous struct is not a Microsoft
Specific one:
C/C++ Language Reference Structure Declarations
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm
/decla_16.asp


By the way, the struct in your code snippet is neither an anonymous one, it
appears having the name "t_fields":
> struct t_fields {
> unsigned __int16 f1 : 16;
> unsigned __int16 f2 : 16;
> };

Steve's struct is really anonymous
struct // anonymous struct
{
unsigned __int16 f1 : 16;
unsigned __int16 f2 : 16;
};


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------


Re: anonymous structs and unions by John

John
Tue May 11 01:22:18 CDT 2004

"Gary Chang" <v-garych@online.microsoft.com> wrote in message
news:MDlY66vNEHA.2720@cpmsftngxa10.phx.gbl
>
> Based on the following MSDN doc, the anonymous struct is not a
> Microsoft Specific one:
> C/C++ Language Reference Structure Declarations
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm
> /decla_16.asp


The link that you give has the following example:

struct somestruct
{
struct
{
int x, y;
} point;
int type;
} w;

MSDN calls this an "anonymous struct". Structs of this type are indeed
allowed in both standard C and standard C++.

Consider, however, what happens if you remove the name "point", giving

struct somestruct
{
struct
{
int x, y;
};
int type;
} w;

This second form is a Microsoft extension. This can be confirmed by using
the /Za switch with VC++ (to disable language extensions). With this switch,
VC++ will NOT compile it as either a C or a C++ file.

MSDN calls this second form an "unnamed anonymous struct". Other sources,
however, call it simply an "anonymous struct" and DON'T use that term for
the first form.

The C++ standard does NOT use the Microsoft terminology. It allows for an
"anonymous union", by which it means what MSDN calls an "unnamed anonymous
union".


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


Re: anonymous structs and unions by Shailesh

Shailesh
Tue May 11 14:20:35 CDT 2004

John Carson wrote:
>
> MSDN calls this second form an "unnamed anonymous struct". Other sources,
> however, call it simply an "anonymous struct" and DON'T use that term for
> the first form.
>
> The C++ standard does NOT use the Microsoft terminology. It allows for an
> "anonymous union", by which it means what MSDN calls an "unnamed anonymous
> union".
>
>
At this URL:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/class_5.asp

It says, "
A Microsoft C extension allows you to declare a structure variable
within another structure without giving it a name. These nested
structures are called anonymous structures. C++ does not allow
anonymous structures.
You can access the members of an anonymous structure as if they were
members in the containing structure.
"

The (condensed) example is:
----------------------
struct phone
{
long number;
};

struct person
{
struct phone; // Anonymous structure; no name needed
} Jim;
----------------------
The "phone" declaration can be moved into the "person" declaration.
----------------------
struct person
{
struct phone
{
long number;
};
} Jim;
---------------------
But now that it's enclosed, there is probably no need to name it:
---------------------
struct person
{
struct
{
long number;
};
} Jim;
----------------------

Correct me if I'm wrong, but with structs, there are four combinations:

(1) type name excluded, identifier excluded - MSFT C Extension
(2) type name included, identifier excluded - MSFT C Extension
(3) type name excluded, identifier included - C/C++ Standard
(4) type name included, identifier included - C/C++ Standard

With unions, all four combinations are allowed in the C/C++ standard.
It would be nice to have some standardized terminology to describe
each of these constructs. From what has been stated so far, my best
guess is:

(1) MSFT unnamed anonymous struct; C/C++ undefined
(2) MSFT and C/C++ - struct definition only
(3) MSFT unnamed struct; C/C++ struct
(4) MSFT and C/C++ - struct definition with declaration



Re: anonymous structs and unions by Alexander

Alexander
Tue May 11 23:07:07 CDT 2004

Note also that is you compile case 2 (nested structure with tag, but no
name) with MSC compiler, in C code it will result in a structure body
included to the outer structure layout. In C++ code, of course, it will just
define a structure type inside the outer structure namespace.
This is why such constructs are better avoided in C code.

"Shailesh Humbad" <humbads1@hotmail.com> wrote in message
news:eJk%23G04NEHA.3124@TK2MSFTNGP12.phx.gbl...
> John Carson wrote:
> >
> > MSDN calls this second form an "unnamed anonymous struct". Other
sources,
> > however, call it simply an "anonymous struct" and DON'T use that term
for
> > the first form.
> >
> > The C++ standard does NOT use the Microsoft terminology. It allows for
an
> > "anonymous union", by which it means what MSDN calls an "unnamed
anonymous
> > union".
> >
> >
> At this URL:
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/class_5.asp
>
> It says, "
> A Microsoft C extension allows you to declare a structure variable
> within another structure without giving it a name. These nested
> structures are called anonymous structures. C++ does not allow
> anonymous structures.
> You can access the members of an anonymous structure as if they were
> members in the containing structure.
> "
>
> The (condensed) example is:
> ----------------------
> struct phone
> {
> long number;
> };
>
> struct person
> {
> struct phone; // Anonymous structure; no name needed
> } Jim;
> ----------------------
> The "phone" declaration can be moved into the "person" declaration.
> ----------------------
> struct person
> {
> struct phone
> {
> long number;
> };
> } Jim;
> ---------------------
> But now that it's enclosed, there is probably no need to name it:
> ---------------------
> struct person
> {
> struct
> {
> long number;
> };
> } Jim;
> ----------------------
>
> Correct me if I'm wrong, but with structs, there are four combinations:
>
> (1) type name excluded, identifier excluded - MSFT C Extension
> (2) type name included, identifier excluded - MSFT C Extension
> (3) type name excluded, identifier included - C/C++ Standard
> (4) type name included, identifier included - C/C++ Standard
>
> With unions, all four combinations are allowed in the C/C++ standard.
> It would be nice to have some standardized terminology to describe
> each of these constructs. From what has been stated so far, my best
> guess is:
>
> (1) MSFT unnamed anonymous struct; C/C++ undefined
> (2) MSFT and C/C++ - struct definition only
> (3) MSFT unnamed struct; C/C++ struct
> (4) MSFT and C/C++ - struct definition with declaration
>
>