Reputation: 42670
I realize I have to put the below code (For template specialization) in CPP file instead of Header file? Is there any way I can make it in Header file?
template<> inline UINT AFXAPI HashKey<const error_code &> (const error_code & e)
{
// Hash code method required for MFC CMap.
// This hash code generation method is picked from Joshua Bloch's
// Effective Java.
unsigned __int64 result = 17;
result = 37 * result + e.hi;
result = 37 * result + e.lo;
return static_cast<UINT>(result);
}
I would get the error if the above function is placed in error_code.h
error C2912: explicit specialization; 'UINT HashKey(const error_code &)' is not a specialization of a function template
Some reference source on why I need to do the above template specialization. http://www.codeproject.com/KB/architecture/cmap_howto.aspx. The below code are picked from the article, and it is part of MFC source code.
// inside <afxtemp.h>
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);
}
Upvotes: 5
Views: 9341
Reputation: 361342
I think you've to do this in your header file.
//template non-specialized version which you forgot to write!
//compiler must know it before the specialized ones!
template<typename T> inline UINT AFXAPI HashKey(T e);
//then do the specializations!
template<> inline UINT AFXAPI HashKey<const error_code &> (const error_code & e)
{
// Hash code method required for MFC CMap.
// This hash code generation method is picked from Joshua Bloch's
// Effective Java.
unsigned __int64 result = 17;
result = 37 * result + e.hi;
result = 37 * result + e.lo;
return static_cast<UINT>(result);
}
EDIT:
After reading your edited part, I think you need to remove the inline
keyword. I'm not sure though. Try doing that. :-)
Upvotes: 5
Reputation: 308121
I think all this means is that you haven't defined the template version of the function before the specialization. I think the best course of action would be to put this in its own header file, and #include
the error.h and hashkey.h files at the front of it. Or you could just have error.h include hashkey.h.
Upvotes: 2