Automatically change between std::string and std::wstring according to unicode setting in MSVC++?

Felix Dombek picture Felix Dombek · May 26, 2011 · Viewed 7.4k times · Source

I'm writing a DLL and want to be able to switch between the unicode and multibyte setting in MSVC++2010. For example, I use _T("string") and LPCTSTR and WIN32_FIND_DATA instead of the -W and -A versions and so on.

Now I want to have std::strings which change between std::string and std::wstring according to the unicode setting. Is that possible? Otherwise, this will probably end up getting extremely complicated.

Answer

bdonlan picture bdonlan · May 26, 2011

Why not do like the Win32 API does: Use wide characters internally, and provide a character-converting facade of DoSomethingA functions which simply convert their input to Unicode.

That said, you could define a tstring type like so:

#ifdef _UNICODE
typedef std::wstring tstring;
#else
typedef std::string tstring;
#endif

or possibly:

typedef std::basic_string<TCHAR> tstring;