views:

5451

answers:

4

You'll have to forgive my ignorance, but I'm not used to using wide character sets in c++, but is there a way that I can use wide string literals in c++ without putting an L in front of each literal?

If so, how?

+1  A: 

Why do you not want to prefix string literals with an L? It's quite simple - strings without an L are ANSI strings (const char*), strings with an L are wide-character strings (const wchar_t*). There is the TEXT() macro, which makes a string literal into an ANSI or a wide-character string depending on of the current project is set to use Uncode:


#ifdef UNICODE
#define TEXT(s) L ## s
#else
#define TEXT(s) s
#endif

There's also the _T() macro, which is equivalent to TEXT().

Adam Rosenfield
We're reading in files that may be in unicode and outputting a unicode xml, so the conversion mapping that we are doing internally compares a wstring to our string literal.
Fry
@Adam--just curious--do you know if the _T and TEXT macros are part of the language standard or are they MS VC++ specific?
Onorio Catenacci
It's in WinNT.h (included from windows.h). I wouldn't consider it as VC++ specific, but rather windows specific as it would be available in any setup that let you build WinAPI apps.
KTC
+15  A: 

No, there isn't. You have to use the L prefix (or a macro such as _T() with VC++ that expands to L anyway when compiled for Unicode).

Ferruccio
That's not entirely accurate. the _T() macro only expands to L in you define UNICODE. that is essentially the point of having it as a macro. if you want the string to always be unicode just use L directly.
shoosh
Yes, of course. I should have mentioned that.
Ferruccio
+8  A: 

The new C++0x Standard defines another way of doing this:
http://en.wikipedia.org/wiki/C%2B%2B0x#New_string_literals

shoosh
Yeah, but that still requires a prefix. BTW, I still voted you up because this is accurate advice.
Max Lybbert
A: 

on a related note.. i'm trying to do the following

#define  get_switch( m )   myclass::getSwitch(L##m)

which is a macro the will expand

get_switch(isrunning)

into

myclass::getswitch(L"isrunning")

this works fine in c++ visualstudio 2008

but when i compile the same code under mac Xcode (for iphone) i get the error:

error: 'L' was not defined in this scope.

EDIT: Solution

#define  get_switch( m )   myclass::getSwitch(L ## #m)

this works on both vc++ and mac xcode (gcc)

ShoeLace
Shouldn't that be `#define get_switch( m ) myclass::getSwitch(L ## #m)'?
D.Shawley
thanks that does indeed work.. i had got it to work with myclass::getSwitch(L"" #m) also
ShoeLace