Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template Specialization in Header File

Tags:

c++

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);
}
like image 975
Cheok Yan Cheng Avatar asked Jan 05 '11 03:01

Cheok Yan Cheng


People also ask

Do templates go in header files?

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.

What is meant by template specialization?

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.

What does template <> mean in C++?

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.

Why are template classes are header only?

If these template class implementations were not in the header, they wouldn't be accessible and hence won't compile.


2 Answers

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. :-)

like image 94
Nawaz Avatar answered Oct 21 '22 21:10

Nawaz


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.

like image 41
Mark Ransom Avatar answered Oct 21 '22 21:10

Mark Ransom