Я пишу класс шаблона String (только для учебных целей) и имею небольшую проблему. Если T - wchar_t и U - char, и наоборот, что мне не хватает для этого метода?Преобразование из wchar_t a char и наоборот
template<typename U>
String<T> operator + (const U* other)
{
String<T> newString;
uint32_t otherLength = length(other);
uint32_t stringLength = m_length + otherLength;
uint32_t totalLength = stringLength * sizeof(T) + sizeof(T);
T *buffer = new T[totalLength];
memset(buffer, 0, totalLength);
memcpy(buffer, m_value, m_length * sizeof(T));
newString.m_value = buffer;
newString.m_length = stringLength;
memcpy(newString.m_value + m_length, other, otherLength * sizeof(T));
return newString;
}
Хорошо, Джаред ниже предложил решение, так что-то вроде этого (есть ошибки, я знаю, просто шаблон)?
template<typename U>
String<T> operator + (const U* other)
{
String<T> newString;
uint32_t sizeOfT = sizeof(T); // wchar_t is 4
uint32_t sizeOfU = sizeof(U); // char is 1
T* convertedString;
int i = 0;
while (*other != 0)
{
convertedString[i] = ConvertChar(*other);
other++;
i++;
}
return newString;
}
template <typename U>
T ConvertChar(U character)
{
}
Если все ваши персонажи ASCII, то немой wchar_t на char хорошо. в любом другом случае вам нужно преобразовать Unicode в UTF8. http://utfcpp.sourceforge.net/ – SHR