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

Re: C4311 and reinterpret_cast by Doug

Doug
Fri Nov 25 11:30:07 CST 2005

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

Re: C4311 and reinterpret_cast by athirumurthi

athirumurthi
Mon Nov 28 11:20:01 CST 2005

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


Re: C4311 and reinterpret_cast by Doug

Doug
Mon Nov 28 13:18:37 CST 2005

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

Re: C4311 and reinterpret_cast by athirumurthi

athirumurthi
Mon Nov 28 13:52:19 CST 2005

> 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