Quote:
Originally Posted by RavindraB
What is the difference between following two templates
template <class T>
T& strcpy1(T &tParam1, T &tParam2)
{
strcpy_s(tParam1, sizeof(tParam2), tParam2);
return tParam1;
}
template <class T>
T strcpy1(T *tParam1, T *tParam2)
{
strcpy_s(tParam1, sizeof(tParam2), tParam2);
return *tParam1;
}
The first declares a function that takes reference arguments the second a function that takes pointer arguments. The correct operation depends entirely on what the compiler chooses to use for class T.
Quote:
Originally Posted by RavindraB
template <int CCH>
inline HRESULT SafeStrCopy(char (&szBuffer)[CCH], const char *szStr)
{
C_ASSERT(CCH > 0);
return StringCchCopy(szBuffer, CCH, szStr);
}
Here its determine array size in CCH field, through “char &szBuffer)[CCH]”
But not cleared well to me.
How compiler calls this template when it invokes strcpy(sz_StrTo,sz_StrFrom);
Though it seems from template decalration that we need to pass arg as int ..?
What is difference between
template <int CCH> and template <class T> declarations?
Careful, template parameters are not arguments. Since sz_StrTo and the function takes a reference to an array of size CCH the compiler can work out the value of CCH required to call the function with sz_StrTo. Alterntitively it is possible that this template was always used with explicit values for it template parameters rather than trying to let the compiler work it out.
The difference between template <int CCH> and template <class T> is that the first declares a template with a generic value and the second declares a template with a generic type.