For instance, I have just found myself writing the following traits like class:
template<class TCHAR> struct sz;
template<> struct sz<char>
{
static void copy(char *dst, int bufSize, const char *src)
{
strcpy_s(dst, bufSize, src);
}
};
template<> struct sz<wchar_t>
{
static void copy(wchar_t *dst, int bufSize, const wchar_t *src)
{
wcscpy_s(dst, bufSize, src);
}
};
I was wondering whether one really has to write such things or is there anything already written that lets us manipulate strings without caring about wchar_t or char?
After all, we have:
coutvswcoutcerrvswcerrstringvswstringboost::formatvsboost::wformat- etc …
Check
std::char_traits<Ch>. In particular,strcpy(dst, src, n)can be rewritten intostd::char_traits<Ch>::copy(dst, src, n).It doesn’t have everything you requested though. Just the standard C string functions are available inside.
For
stringvswstring, you could usestd::basic_string<Ch>, which is what these two typedefs actually are. Similarly, we haveboost::basic_format<Ch>.