- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
"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.
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Use:
m_union = m_int;
or:
m_union.raw = m_int.raw;
or (barf):
m_union = *((union_t*)(&m_int));
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Also, you may need to insert a compiler memory fence too.
Jim Dempsey
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
I have added this option to the Visual studio compialtion options and i am getting the same errors
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page