Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Error on cast to union type

yanirdz
Beginner
1,078 Views
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
1,078 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
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,078 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
0 Kudos
TimP
Honored Contributor III
1,078 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.
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,078 Views
Also, you may need to insert a compiler memory fence too.

Jim Dempsey
0 Kudos
yanirdz
Beginner
1,078 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
0 Kudos
Reply