Is the following use of the template-function fCompare()
correct?
//header
template<typename _T > class SomeClass
{
typedef int (*COMPAREFUNC)(_T*,_T*);
COMPAREFUNC Compare;
public:
void SetCompareFunction(COMPAREFUNC pfC) { Compare=pfC; }
...
};
template<typename _T > int fCompare(_T *pO, _T *pN)
{
if (pN==NULL) throw (const char*)"Null not allowed";
if (pO!=NULL) return (*pO > *pN)?1:(*pO < *pN)?(-1):0;
return 0;
}
//code
SomeClass<int> aC;
aC.SetCompareFunction(fCompare<int>); // <******* here
...
My worry is where the instance function is created from the template: it looks like the stack, but in other code I used to test it, I tried to hammer the stack, and the Compare()
just kept on going. Can I safely use the template like this?