Home > Archive > VC Language > November 2005 > C4311 and reinterpret_cast
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
C4311 and reinterpret_cast
|
|
| athirumurthi@yahoo.ca 2005-11-25, 7:02 pm |
| I'm in the unfortunate situation of needing to interface with a library
that I can't change. To document the non-portable typecasts, I have
been using cast operators. However, I'm unable to silence C4311 with
reinterpret_cast. No other cast operator is appropriate. Is there a
two-step mechanism to deal with C4311 without offending VC++ 7.1?
CMsgReceiverQueueIterator itr = m_qParentReceiverList.begin ();
while ( itr != m_qParentReceiverList.end () )
{
CNkvMessageReceiver * thisRec = *itr;
// warning C4311: 'reinterpret_cast' : pointer truncation from
'std::allocator<_Ty>::value_type' to 'ULONG'
NkvMessage (reinterpret_cast <ULONG> (*itr));
itr++;
}
Thanks in advance,
- Ashok Thirumurthi
| |
| Doug Harrison [MVP] 2005-11-25, 7:02 pm |
| On 25 Nov 2005 09:11:06 -0800, athirumurthi@yahoo.ca wrote:
>I'm in the unfortunate situation of needing to interface with a library
>that I can't change. To document the non-portable typecasts, I have
>been using cast operators. However, I'm unable to silence C4311 with
>reinterpret_cast. No other cast operator is appropriate. Is there a
>two-step mechanism to deal with C4311 without offending VC++ 7.1?
>
>CMsgReceiverQueueIterator itr = m_qParentReceiverList.begin ();
>
>while ( itr != m_qParentReceiverList.end () )
>{
> CNkvMessageReceiver * thisRec = *itr;
>
> // warning C4311: 'reinterpret_cast' : pointer truncation from
>'std::allocator<_Ty>::value_type' to 'ULONG'
>
> NkvMessage (reinterpret_cast <ULONG> (*itr));
>
> itr++;
>}
That's a 64-bit portability warning. You can avoid the warning by:
1. Not compiling with -Wp64, or
2. Probably by using a #pragma, or
3. Casting to ULONG_PTR.
You can avoid the problem in the future by using ULONG_PTR (or similar
type) as your integer type for holding pointers.
--
Doug Harrison
Visual C++ MVP
| |
| athirumurthi@yahoo.ca 2005-11-28, 7:07 pm |
| Thanks for the hint. How about:
NkvMessage (static_cast <ULONG> (reinterpret_cast <ULONG_PTR> (*itr)));
or the more portable:
NkvMessage (static_cast <ULONG> (reinterpret_cast <intptr_t> (*itr)));
The compiler no longer generates C4311 so I can clearly identify new
warnings and continue to remedy non-portable casts when I'm not
interfacing to old libraries. Also, I can search for
'reinterpret_cast' when the need for 64-bit portability takes hold.
Essentially, I document the transgressions.
- Ashok Thirumurthi
| |
| Doug Harrison [MVP] 2005-11-28, 7:07 pm |
| On 28 Nov 2005 09:20:01 -0800, athirumurthi@yahoo.ca wrote:
>Thanks for the hint. How about :
>
>NkvMessage (static_cast <ULONG> (reinterpret_cast <ULONG_PTR> (*itr)));
>
>or the more portable:
>
>NkvMessage (static_cast <ULONG> (reinterpret_cast <intptr_t> (*itr)));
>
>The compiler no longer generates C4311 so I can clearly identify new
>warnings and continue to remedy non-portable casts when I'm not
>interfacing to old libraries. Also, I can search for
>'reinterpret_cast' when the need for 64-bit portability takes hold.
>Essentially, I document the transgressions.
Looks fine. Just remember the integer types ULONG_PTR and intptr_t really
are 64 bits on Win64, as are pointers, while ULONG remains 32 bits, and
you're discarding the upper 32 bits of your pointers.
--
Doug Harrison
Visual C++ MVP
| |
| athirumurthi@yahoo.ca 2005-11-28, 7:07 pm |
| > Just remember the integer types ULONG_PTR and intptr_t really
are 64 bits on Win64, as are pointers, while ULONG remains 32 bits <
Your targetted comment encouraged me to add an assert statement.
Thanks,
- Ashok Thirumurthi
|
|
|
|
|