Reputation: 62886
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:
cout
vs wcout
cerr
vs wcerr
string
vs wstring
boost::format
vs boost::wformat
Upvotes: 1
Views: 76
Reputation: 523724
Check std::char_traits<Ch>
. In particular, strcpy(dst, src, n)
can be rewritten into std::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 string
vs wstring
, you could use std::basic_string<Ch>
, which is what these two typedefs actually are. Similarly, we have boost::basic_format<Ch>
.
Upvotes: 2