using union keywords

S

steve.kim

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
 
R

Rolf Magnus

steve.kim said:
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.
 
G

Gavin Deane

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
 
S

steve.kim

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
 
D

dasjotre

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.
 
G

Gavin Deane

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
 
M

Marcus Kwok

Gavin Deane said:
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.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top