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.
String overview std::string is used for standard ascii and utf-8 strings. std::wstring is used for wide-character/unicode (utf-16) strings.
@MSalters: std::string can hold 100% of all Unicode characters, even if CHAR_BIT is 8. It depends on the encoding of std::string, which may be UTF-8 on the system level (like almost everywhere except for windows) or on your application level.
std::string is the string class from the standard C++ library. String is some other string class from some other library. It's hard to say from which library, because there are many different libraries that have their own class called String.
UTF-8 actually works quite well in std::string . Most operations work out of the box because the UTF-8 encoding is self-synchronizing and backward compatible with ASCII.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With