By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,031 Members | 1,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,031 IT Pros & Developers. It's quick & easy.

using union keywords

P: n/a
Hello,

I'm trying to make a class like below...

class myClass {
public:
// ctor / dtor
....

// methods
....

public:
// public data member
union {
double data[3];
struct { double A, B, C; };
}
private:
// private data member
...
};
I found that this data member declaration is illegal in ANSI C++ but
legal in VC.

Do you have any idea in using union keyword to data[3] and struct {}
legally in ANSI C++?

this layout is just for using the data members like below

myClass my;
my.A = SOME_DOUBLE_PRECISION_VALUE;

not using
my.data[0] = ... ; // same my.A

Mar 2 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
steve.kim wrote:
Hello,

I'm trying to make a class like below...

class myClass {
public:
// ctor / dtor
....

// methods
....

public:
// public data member
union {
double data[3];
struct { double A, B, C; };
}
private:
// private data member
...
};
I found that this data member declaration is illegal in ANSI C++ but
legal in VC.
Well, there are no anonymous structs in C++.
Do you have any idea in using union keyword to data[3] and struct {}
legally in ANSI C++?
Give the struct member a name.

Mar 2 '07 #2

P: n/a
On 2 Mar, 08:49, "steve.kim" <precisionma...@gmail.comwrote:
Hello,

I'm trying to make a class like below...

class myClass {
public:
// ctor / dtor
....

// methods
....

public:
// public data member
union {
double data[3];
struct { double A, B, C; };
}
private:
// private data member
...

};

I found that this data member declaration is illegal in ANSI C++ but
legal in VC.
Two things:
1. You haven't given the struct type a name. An anonymous struct is
simply wrong.
2. Even if you correct that, anonymous unions are not allowed to
contain nested types.
Do you have any idea in using union keyword to data[3] and struct {}
legally in ANSI C++?
Would this work for you? I've not executed any code using it, but it
compiles with Comeau online:

class myClass {
public:
// public data member
struct myStruct { double A, B, C; };
union {
double data[3];
myStruct s;
};
};
this layout is just for using the data members like below

myClass my;
my.A = SOME_DOUBLE_PRECISION_VALUE;

not using
my.data[0] = ... ; // same my.A
Now my.s.A is the same thing as my.data[0]

Gavin Deane
Mar 2 '07 #3

P: n/a
thanks for the response.

I found that my union declaration is only legal with MSVC++ extension.

this strange compiler accepts the codes like below -_-;;

struct s {
int a;
struct { int b, c, d;};
};

without MSVC++ extensions the compiler never accept the codes.

as well as my union declaration.

I think there is two C++... ANSI C++ and MSVC++.

I realized that something wrong in standards is not wrong in my
Windows platform.

thanks

steve from KR

Mar 2 '07 #4

P: n/a
On 2 Mar, 10:30, "steve.kim" <precisionma...@gmail.comwrote:
thanks for the response.

I found that my union declaration is only legal with MSVC++ extension.

this strange compiler accepts the codes like below -_-;;

struct s {
int a;
struct { int b, c, d;};

};

without MSVC++ extensions the compiler never accept the codes.

as well as my union declaration.

I think there is two C++... ANSI C++ and MSVC++.

I realized that something wrong in standards is not wrong in my
Windows platform.
you can always use /Za switch to disable microsoft specific
extensions.
Mar 2 '07 #5

P: n/a
On 2 Mar, 10:30, "steve.kim" <precisionma...@gmail.comwrote:

<snip>
I think there is two C++... ANSI C++ and MSVC++.
There are two reasons why a complier might accept non-standard code: a
compiler bug (the compiler author did not mean for it to be accepted)
or a deliberate extension to the language (the compiler author did
mean for it to be accepted). Many implementations of C++ provide
extensions, presumably because the implementor thought they might be
useful to customers. And if you do find an extension useful there is
nothing intrinsically wrong with using it. You just need to bear in
mind that if you try and use that extension on a different
implementation (either when writing a new program or when porting your
existing code), it might not work. Only you know whether that is a
problem for your particular project. And, of course, discussion of
specific extensions belongs in and is most productive in a group
dedicated to the particular extension.

As suggested elsethread, implementations that provide extensions often
also document a means of disabling them.

Gavin Deane

Mar 2 '07 #6

P: n/a
Gavin Deane <de*********@hotmail.comwrote:
On 2 Mar, 08:49, "steve.kim" <precisionma...@gmail.comwrote:
class myClass {
public:
// public data member
struct myStruct { double A, B, C; };
union {
double data[3];
myStruct s;
};
};
>this layout is just for using the data members like below

myClass my;
my.A = SOME_DOUBLE_PRECISION_VALUE;

not using
my.data[0] = ... ; // same my.A

Now my.s.A is the same thing as my.data[0]
It is undefined behavior to assign to one member of a union and then try
to read its value from a different member, but I think there are
exceptions to that rule (common initial sequence PODs or something). I
am not sure if this falls into that category or not, since it might
align the elements of the array differently from the members of the
struct.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.