error C2440: 'specialization' : cannot convert from 'int (__stdcall *)(void *)' to ' (__stdcall *)()'
船长77 2005-05-01 09:43:44 template<class T, class CloseT, CloseT (_stdcall * CloseFunc)(T)> class THandle {
T m_h;
public:
THandle() {
m_h = T(-1); // Always illegal
}
THandle(T h) { m_h = h; }
~THandle() {
if (IsValid()) Close(); // Ensure we do not double-close
}
THandle<T, CloseT, CloseFunc>& operator=(T h) {
m_h = h;
return *this;
}
operator T() { return m_h; }
T *operator &() { return &m_h; }
CloseT Close() {
T h = m_h;
m_h = T(-1);
return (*CloseFunc)(h); // Return the result of whatever type
};
BOOL IsValid() { return m_h != T(-1) && m_h != T(0); }
};
//
// Now define common handles as safe classes.
//
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHandle;
typedef class THandle<HKEY, LONG, ®CloseKey> CHKey;
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHFile;
typedef class THandle<HANDLE, BOOL, &FindClose> CHFind;
typedef class THandle<HANDLE, BOOL, &FindCloseChangeNotification> CHChange;
typedef class THandle<HMODULE, BOOL, &FreeLibrary> CHModule;
typedef class THandle<HWND, BOOL, &DestroyWindow> CHWnd;
//
代码见上,VC6编译的时候报
error C2440: 'specialization' : cannot convert from 'int (__stdcall *)(void *)' to ' (__stdcall *)()'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
error C2975: 'THandle' : invalid template argument for 'CloseFunc', constant expression expected see declaration of 'THandle'
.......
WHY?