Re: Make std::cout accept std::wstring ?
Ali ??ehreli wrote:
On Oct 9, 12:35 pm, jyoti <jyoti.mic...@gmail.com> wrote:
On Fri, 09 Oct 2009 08:33:17 +0530, Martin B. <0xCDCDC...@gmx.at> wrote:
* Where / which operator<< would need to be specialized or overloaded?
* I would need to convert from (a known) unicode encoding to (a known)
multibyte encoding and then feed that to cout?
* Are there any stdlib implementations out there that already support
this?
jyoti@jyoti-desktop:~$ cat test.cpp
#include <iostream>
int main(void)
{ std::wstring str = L"Hello World.\n";
std::wcout << str;
return 0;}
jyoti@jyoti-desktop:~$ g++ --pedantic -Wall test.cpp
jyoti@jyoti-desktop:~$ ./a.out
Hello World.
All ASCII characters? I am not impressed... :/ What is wstring good
for then? What have we gained?
How is wstring better than an array of integers?
FWIW, my OP was about outputting wstring to cout and *not wcout* and the
problems one might face.
Did you bother yourself with a google search?
Challenge: Write a C++ program that takes the user's name, compares it
to a hard-coded name that which contains a Unicode character, e.g.
"??ule". And produces either "Seni tan??yorum!" (I know you) for a
match, or "Tan????t??????m??za memnun oldum" (nice to meet you) for no
match.
Simple student program... Google all you want.
I am very curious on how to write this program portably. I am also
very skeptical. I don't think C++ is for non-ASCII alphabets at all.
Oversimplification.
The problem with portability here is not so much (standard) C++ I think.
Even if you restrict yourself to a subset of C++ compilers that will
correctly handle unicode (UTF-16 or UTF-32) for wstring, you still have
the problem that you have to know where the string is going to and that
the stdlib implementation on the platform needs to correctly convert the
wstring to the appropriate encoding of the "display device" (i.e. either
the console or some graphical library).
For example on Windows:
* For GUI programs you have no problems. You feed the unicode characters
(be they in a wstring or a CString) to the Win API and they will be
correctly displayed (*if* you have set a font that has the characters)
* For console programs you're pretty much f'ed because while you can get
it to work it's so horribly complicated that I'd call it broken.
(see a posting of mine in m.p.v.stl:
http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.vc.stl&tid=db2fda2b-0423-4b5b-84f6-7c10b56ffefa&cat=en_US_ef479e33-8471-4f51-8ac6-ddac3f808cbf&lang=en&cr=US&sloc=&p=1
)
So what is one to do?
One solution I could think of for the console, (I have never tried it )
is to write an operator<<(std::ostream, std::wstring) that will convert
the wstring from the platform specific wide unicode encoding to UTF-8.
Of course that would mean that cout should expect UTF-8 so you can't use
it with the "normal" ASCII codepage.
br,
Martin
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]