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);
}
To have all the information available, current compilers tend to require that a template must be fully defined whenever it is used. That includes all of its member functions and all template functions called from those. Consequently, template writers tend to place template definition in header files.
The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.
Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.
If these template class implementations were not in the header, they wouldn't be accessible and hence won't compile.
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. :-)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With