Consider this code:
template <typename T>
class String
{
public:
...
String(T* initStr)
{
size_t initStrLen;
if (initStr != NULL)
{
printf_s("%s\n", typeid(T) == typeid(char) ? "char" : "wchar_t");
if (typeid(T) == typeid(char))
{
strlen((T*)initStr);
}
else if (typeid(T) == typeid(wchar_t))
{
wcslen((T*)initStr);
}
}
}
...
};
When I compiled the code, I got this error message:
...\main.cpp(32) : error C2664: 'strlen' : cannot convert parameter 1 from 'wchar_t *' to 'const char *'
Then I tried to use a function pointer:
typedef size_t (*STRLEN)(void*);
STRLEN _strlen;
_strlen = reinterpret_cast<STRLEN> (typeid(*initStr) == typeid(char) ? strlen : wcslen);
and again the compiler issued an error, this time:
...\main.cpp(28) : error C2446: ':' : no conversion from 'size_t (__cdecl *)(const wchar_t *)' to 'size_t (__cdecl *)(const char *)'
My question is, how can I use the functions strlen
and wcslen
with templates?