mark
mark

Reputation: 62886

C++: Is there a traits class containing various functions to manipulate zero terminated char * and wchar_t * strings?

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:

Upvotes: 1

Views: 76

Answers (1)

kennytm
kennytm

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

Related Questions