ascii "" and unicode L"" question

M

mr

How can i 'force' c++ to interprete "blabla" strings as unicode string
instead of ascii string (i just don't want to add 'L' before the thousands
strings that are on my projects...), as all my projects are using unicode,
and i don't see any reason that c++ compilator keep creating ascii

Is it a way?

thanks !
 
A

Alf P. Steinbach

* mr:
How can i 'force' c++ to interprete "blabla" strings as unicode string
instead of ascii string (i just don't want to add 'L' before the thousands
strings that are on my projects...), as all my projects are using unicode,
and i don't see any reason that c++ compilator keep creating ascii

Is it a way?

You can not force Unicode in any portable way in C++.

You can change all your string literals to wide string literals by
running a script or C++ program that prepends L to every string literal.

But in addition you need to replace char with wchar_t, std::string with
std::wstring, and so on, and if you have calls to library functions that
expect char-based arguments you'll need to fix those calls to (I suggest
leaving exceptions as char-based because Unicode exceptions have no
advantages that I know of, and many drawbacks).

Also, at least one popular compiler doesn't support std::wcout & family,
so you'll have to use some workaround (this includes a decision about
how to handle i/o).

Good luck: it's a bit of work, but it can pay off.
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

mr said:
How can i 'force' c++ to interprete "blabla" strings as unicode string
instead of ascii string (i just don't want to add 'L' before the thousands
strings that are on my projects...), as all my projects are using unicode,
and i don't see any reason that c++ compilator keep creating ascii

Is it a way?

thanks !

I would have to agree with the other poster, adding the L before string
and character literals is the least of your problems, but a good place
to start.

I have already done this for a huge project and it is a lot of work.
You need to examine every single place you use strings and determine
what the correct types are. Some places you must still use narrow
strings (such as network protocols and most file IO). In other places
the format of the wide and narrow strings will depend on other factors
and you will need to write code to narrow and widen strings correctly
taking these factors into account.

To correctly support Unicode is more than just turning on wide
character support though. std::wstring is not able to handle Unicode
strings correctly (not any better than std::string can).

How much work you need to do depends a lot on what the application does
and is one of the reasons why you will need to examine every use of a
string or character (remember for example that wchar_t on most
platforms is not big enough for one Unicode character - you need 32
bits, and that's assuming you can avoid any issues of
canonicalisation).

My tip is to start with all the code that performs IO and then look at
any code that makes use of third party APIs. Thin wrappers for all of
these places are often a good way to handle all of this.
 
F

Frederick Gotham

mr posted:
How can i 'force' c++ to interprete "blabla" strings as unicode
string instead of ascii string (i just don't want to add 'L' before
the thousands strings that are on my projects...), as all my projects
are using unicode, and i don't see any reason that c++ compilator keep
creating ascii

Is it a way?


What I do is define the following:

#ifdef WIDE
#define LITERAL(x) L##x
#else
#define LITERAL(x) x
#endif


And write code as follows:

SomeFunc( 5, 3, LITERAL("Version 7"), 3 );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top