Is this legal code?

E

Ed J

I'm trying to port some C++ code from MS Visual Studio 6.0 to a version of
Code Composer Studio for an embedded application.

This code compiles under Visual Studio, and lets the program reference x, y,
and z. Under a Code Composer Studio compiler, the compiler warns: "this
declaration doesn't declare anything", then gives errors for all subsequent
references to x, y, and z.

union{
struct{
float x, y, z; // Named element representation
};
float v[3]; // Vector component
};

My C++ book implies that the struct declaration shouldn't declare any data
because there is no assigned name, and I believe that is why Code Composer
is complaining.

Is this some legal C++ extension that is just not supported by Code
Composer, or is it a non-standard Visual Studio extension?

Ed.
 
J

John Harrison

Ed J said:
I'm trying to port some C++ code from MS Visual Studio 6.0 to a version of
Code Composer Studio for an embedded application.

This code compiles under Visual Studio, and lets the program reference x, y,
and z. Under a Code Composer Studio compiler, the compiler warns: "this
declaration doesn't declare anything", then gives errors for all subsequent
references to x, y, and z.

union{
struct{
float x, y, z; // Named element representation
};
float v[3]; // Vector component
};

My C++ book implies that the struct declaration shouldn't declare any data
because there is no assigned name, and I believe that is why Code Composer
is complaining.

Is this some legal C++ extension that is just not supported by Code
Composer, or is it a non-standard Visual Studio extension?

Ed.

It's usually called an anonymous struct, it's not standard C++. Anonymous
unions on the other hand are standard.

john
 
E

edA-qa mort-ora-y

Ed said:
union{
struct{
float x, y, z; // Named element representation
};
float v[3]; // Vector component
};

A few points:
1. Nested types aren't allowed to be declared in anonymous unions (but
you could work around that with a non-nested type)
2. At global/namepsace scope the union must be declared static
3. Although v and x,y,z occupy the same memory space, there is no
guarnatee the the three values of v are the same values as x,y,z
(alignment requirements could align the variables differently)
 
J

JKop

Ed J posted:
union{
struct{
float x, y, z; // Named element representation
};
float v[3]; // Vector component
};


This would be exactly like writing:


int;
float;


You're not naming the variable, nor are you defining a union. The following
would be valid:


union Cat {
struct {
float x, y, z;
};

float v[3];
};

Here I have defined a union... (I hasten to use the term "template"!)...
from which I can in the future *declare* variables, as in:

Cat cat;

OR, going down another road:

union {
struct {
float x, y ,z;
};

float v[3];
} cat;


Your original code would only be valid in the following circumstance:

struct Choc {
union {
struct {
float x, y, z;
};

float v[3];
};
};


-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top