Community
cancel
Showing results for 
Search instead for 
Did you mean: 
yanirdz
Beginner
296 Views

Error on cast to union type

I am getting the following error in Visual studio using ICC, in UNIX there are no errors:
"error: cast to type (some union type) is not allowed"

this is an example of the code i am compilimg:
"
typdef union
{
struct
{
unsigned __int64 u1 :1;
unsigned __int64 u2 :63;
}
unsigned __int64 raw;
}union_t;

main()
{
unsigned __int64 m_int = 0x3212;
union_t m_union;
m_union = (union_t)m_int
}
"

I cannot change the code, so i need a solution to pass compilation in Visual studio.
0 Kudos
5 Replies
Judith_W_Intel
Employee
296 Views


ISO C forbids casts to union type. We allow it on Linux (for compatibility with gcc), but Microsoft does not allow it so we do not allow it on Windows (for compatibility with Microsoft C).

I don't know of any solution that does not involve changing the code to something like:

//m_union = (union_t)m_int;
m_union.raw = m_int;

Judy
jimdempseyatthecove
Black Belt
296 Views

You are missing ";" at end of struct brace. (likely not the problem).

Use:

m_union = m_int;
or:
m_union.raw = m_int.raw;

or (barf):

m_union = *((union_t*)(&m_int));

Jim Dempsey
TimP
Black Belt
296 Views

I would have thought these casts should give you complaints under the default gcc option -fstrict-aliasing. The option could be changed to permit them (e.g. not setting the preferred icc option -ansi_alias) at the expense of forcing the compiler to skip optimizations.
jimdempseyatthecove
Black Belt
296 Views

Also, you may need to insert a compiler memory fence too.

Jim Dempsey
yanirdz
Beginner
296 Views

Are you claiming i can use -Qansi-alias- to solve those compialtion error?
I have added this option to the Visual studio compialtion options and i am getting the same errors
Reply