why the type castings (UINT)(void*)(DWORD) are needed?

c, c++, casting, mfc

Solution

template<class ARG_KEY>
AFX_INLINE UINT AFXAPI HashKey(ARG_KEY key)
{
    // default identity hash - works for most primitive values
    return (DWORD)(((DWORD_PTR)key)>>4);
}

That's what that function looks like today. Your version came from a very old version of MFC, old enough to still support 16-bit programs. MFC was first released in 1992, the days of Windows version 3. MFC versions 1.0 through 2.5 supported 16-bit targets. The current version of the function is good for 32-bit and 64-bit code.

In 16-bit code, one option to select was the memory model. You could pick cheap 16-bit near pointers or expensive 32-bit far pointers. So the extra void* cast trims the value to the memory model size.

Problem

This is a default HashKey function in MFC's CMap class. ``` AFX_INLINE UINT AFXAPI HashKey(ARG_KEY key) {   // default identity hash - works for most primitive values   return ((UINT)(void*)(DWORD)key) >> 4; } ``` My question is why the type casting (DWORD) and (void*) are needed?. I guess the (DWORD) may have some relationship with compatibility affairs for 16-bit machines. But I'm confused about the void*.

Original source