views:

359

answers:

4

Is there a way to make all character sequences UNICODE by default? For instance, now I have to say:

std::wstring wstr(L"rofl");

instead, I'd like to say

std::wstring wstr("rofl");

Thanks!

Visual C++ 8.0

+8  A: 

No, you have to use L"" in order to specify a wide-string literal.

dalle
+2  A: 

No, there is no such thing. Many platforms intended for compiliation with either Unicode or ASCII (but not both) provide a macros to allow easy selection of the desired character type (often _ is used, e.g. _("text")). You can use this if your issue is compiling for both options. Otherwise, it's only an additional character.

Also, bear in mind the exact encoding of wchar_t may vary from platform to platform. Sometimes it is UTF-16, sometimes it is UTF-32. C++1x will add explicit encoding-specified strings u8"string" u"string" and U"string". (I could be wrong about the prefixes, but there will be such strings).

coppro
A: 

I was surprised by this: Doesn't wstring have a wstring(const char *) constructor??

Roddy
No, it has a wstring(const wchar_t*) constructor. It also has a constructor from a pair of iterators, but this is inappropriate in the case of a string literal.
coppro
Nope. It has basic_string<T>(const T*), where T = wchar_t.
Rob Kennedy
Perhaps; the standard doesn't require it, but it doesn't forbid it either.
MSalters
A: 

As an aside, CString supports cross-width construction (eg: CString("text") will work for Unicode builds, and CString(L"text") for MBCS builds), but still does a conversion in both cases. That might accomplish what you want, at the cost of performance.

I'd suggest getting used to using _T("text"), as that's generally what you want.

Nick