473,583 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::wcout and const char *

Hi!

In converting applications from ansi to unicode, it is a problem that
the std::wcout accepts a const char * without complaining compile time.

This compiles and runs:
------------------------------
const char *lookup()
{
return "Hello world";
}
std::wcout << lookup();
------------------------------

I would like it to fail compile time, so I can properly convert cases
like this to unicode in a meaningful way.

Any ideas?
Regards,
Erik Knudsen
Dec 11 '07 #1
10 3390
On 12/11/2007 10:16 AM, Erik Knudsen wrote:
Hi!

In converting applications from ansi to unicode, it is a problem that
the std::wcout accepts a const char * without complaining compile time.

This compiles and runs:
------------------------------
const char *lookup()
{
return "Hello world";
}
std::wcout << lookup();
------------------------------

I would like it to fail compile time, so I can properly convert cases
like this to unicode in a meaningful way.

Any ideas?
Declare and do not define std::operator<< (wostream&, char const*) yourself?

namespace std
{
inline
wostream& operator<<(wost ream& wos, const char*);
}
Just an idea...
S.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
Dec 11 '07 #2
Stefan Naewe wrote:
>I would like it to fail compile time, so I can properly convert cases
like this to unicode in a meaningful way.

Any ideas?

Declare and do not define std::operator<< (wostream&, char const*) yourself?

namespace std
{
inline
wostream& operator<<(wost ream& wos, const char*);
}
Just an idea...

Thanks, but wouldn't that just give me a linker error, with no reference
to code lines?
Regards,
Erik Knudsen
Dec 12 '07 #3
On 12/12/2007 10:01 AM, Erik Knudsen wrote:
Stefan Naewe wrote:
>>I would like it to fail compile time, so I can properly convert cases
like this to unicode in a meaningful way.

Any ideas?

Declare and do not define std::operator<< (wostream&, char const*)
yourself?

namespace std
{
inline
wostream& operator<<(wost ream& wos, const char*);
}
Just an idea...


Thanks, but wouldn't that just give me a linker error, with no reference
to code lines?
Yes...but:

If I call 'g++ -g' the linker gives me the file and line number:
g++ -g -Wall wchar.cc -o wchar
/tmp/ccLf8VPM.o: In function `main':
wchar.cc:37: undefined reference to `std::operator< <(std::basic_os tream<wchar_t, std::char_trait s<wchar_t&, char const*)'
collect2: ld returned 1 exit status
make: *** [wchar] Fehler 1
HTH

S.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
Dec 12 '07 #4
On 12/12/2007 10:11 AM, Stefan Naewe wrote:
On 12/12/2007 10:01 AM, Erik Knudsen wrote:
>Stefan Naewe wrote:
>>>I would like it to fail compile time, so I can properly convert cases
like this to unicode in a meaningful way.

Any ideas?
Declare and do not define std::operator<< (wostream&, char const*)
yourself?

namespace std
{
inline
wostream& operator<<(wost ream& wos, const char*);
}
Just an idea...

Thanks, but wouldn't that just give me a linker error, with no reference
to code lines?

Yes...but:

If I call 'g++ -g' ...
and remove the 'inline'...
the linker gives me the file and line number:
g++ -g -Wall wchar.cc -o wchar
/tmp/ccLf8VPM.o: In function `main':
wchar.cc:37: undefined reference to `std::operator< <(std::basic_os tream<wchar_t, std::char_trait s<wchar_t&, char const*)'
collect2: ld returned 1 exit status
make: *** [wchar] Fehler 1
I don't know why I put 'inline' there a all...

S.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
Dec 12 '07 #5
On Dec 11, 10:51 am, Stefan Naewe <nos...@please. netwrote:
On 12/11/2007 10:16 AM, Erik Knudsen wrote:
In converting applications from ansi to unicode, it is a
problem that the std::wcout accepts a const char * without
complaining compile time.
This compiles and runs:
------------------------------
const char *lookup()
{
return "Hello world";
}
std::wcout << lookup();
------------------------------
I would like it to fail compile time, so I can properly
convert cases like this to unicode in a meaningful way.
Any ideas?
Declare and do not define std::operator<< (wostream&, char
const*) yourself?
namespace std
{
inline
wostream& operator<<(wost ream& wos, const char*);
}
Just an idea...
That's undefined behavior. You're not allowed to define new
free functions in a std::.

The standard std::wostream defines an operator<< for char
const*. You have to assume that some of the other operator<<
for wostream may use it, so you really have to leave it.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 12 '07 #6
On 12/12/2007 1:41 PM, James Kanze wrote:
On Dec 11, 10:51 am, Stefan Naewe <nos...@please. netwrote:
>On 12/11/2007 10:16 AM, Erik Knudsen wrote:
>>In converting applications from ansi to unicode, it is a
problem that the std::wcout accepts a const char * without
complaining compile time.
>>This compiles and runs:
------------------------------
const char *lookup()
{
return "Hello world";
}
std::wcout << lookup();
------------------------------
>>I would like it to fail compile time, so I can properly
convert cases like this to unicode in a meaningful way.
>>Any ideas?
>Declare and do not define std::operator<< (wostream&, char
const*) yourself?
>namespace std
{
inline
wostream& operator<<(wost ream& wos, const char*);
}
>Just an idea...

That's undefined behavior. You're not allowed to define new
free functions in a std::.
I knew someone would say that...

But really: Who cares in this case if it just works (with some
modifications and only (?) with g++ (see my other post)) ?

The standard std::wostream defines an operator<< for char
const*. You have to assume that some of the other operator<<
for wostream may use it, so you really have to leave it.

S.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
Dec 12 '07 #7
On Dec 12, 2:40 pm, Stefan Naewe <nos...@please. netwrote:
On 12/12/2007 1:41 PM, James Kanze wrote:
On Dec 11, 10:51 am, Stefan Naewe <nos...@please. netwrote:
On 12/11/2007 10:16 AM, Erik Knudsen wrote:
>In converting applications from ansi to unicode, it is a
problem that the std::wcout accepts a const char * without
complaining compile time.
>This compiles and runs:
------------------------------
const char *lookup()
{
return "Hello world";
}
std::wcout << lookup();
------------------------------
>I would like it to fail compile time, so I can properly
convert cases like this to unicode in a meaningful way.
>Any ideas?
Declare and do not define std::operator<< (wostream&, char
const*) yourself?
namespace std
{
inline
wostream& operator<<(wost ream& wos, const char*);
}
Just an idea...
That's undefined behavior. You're not allowed to define new
free functions in a std::.
I knew someone would say that...
But really: Who cares in this case if it just works (with some
modifications and only (?) with g++ (see my other post)) ?
Does it work? It might, or it might not. Or it might work some
of the time, and not others. There are things you can
reasonably define in std::, and expect to get away with it. But
this isn't one of them.
The standard std::wostream defines an operator<< for char
const*. You have to assume that some of the other
operator<< for wostream may use it, so you really have to
leave it.
And that is why. If you declare your version of the function,
either:

-- The only version in the standard library is the template
version. Your version overloads it, and since it is not a
template, has precedence in operator overload resolution.
If the library has overloaded the << operator for, say,
complex with something like:

template< typename Float, typename CharT ... >
std::basic_ostr eam< CharT ... >&
operator<<( std::basic_ostr eam< CharT ... >& dest,
std::complex< Float const& z )
{
dest << "(" << z.real() << "," << z.imag() << ")" ;
return dest ;
}

then this function may call your version, rather than the
standard one; which one will be chosen more or less
randomly.

-- If the standard library has also declared the function
(which would seem a more or less likely "optimizati on" for
the usual cases of std::ostream and std::wostream), then
your declaration just duplicates the one in the standard
library, and doesn't change anything.

The standard limits what you can do in std:: for a very real
reason.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 12 '07 #8
On 2007-12-12 10:28:09 -0500, James Kanze <ja*********@gm ail.comsaid:
On Dec 12, 2:40 pm, Stefan Naewe <nos...@please. netwrote:
>But really: Who cares in this case if it just works (with some
modification s and only (?) with g++ (see my other post)) ?

Does it work? It might, or it might not. Or it might work some
of the time, and not others. There are things you can
reasonably define in std::, and expect to get away with it. But
this isn't one of them.
But as a short-term debugging aid, this is fine: if it detects
problems, you fix them. Once you've done that, you remove it. I've
occasionally gone so far as to add #error statements to standard
headers when I couldn't figure out why some header was being included
in a bogus context. (One of the Windows headers pulls in <stdlib.h>
while inside an extern "C" block. Sigh.)

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 12 '07 #9
Pete Becker wrote:
>
But as a short-term debugging aid, this is fine: if it detects problems,
you fix them. Once you've done that, you remove it. I've occasionally
gone so far as to add #error statements to standard headers when I
couldn't figure out why some header was being included in a bogus
context. (One of the Windows headers pulls in <stdlib.hwhil e inside an
extern "C" block. Sigh.)

I will only use it as a short term porting/debugging aid. However, I use
VC++ 2005, and it doesn't give me the line numbers for a link error.
Regards,
Erik Knudsen
Dec 13 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3063
by: Alex Mizrahi | last post by:
Hello, All! i admit that it's better to ask questions connected with atl/mfc classes in special newsgroups, but seems like people there are interested more in discussing stuff like MFC GUI than C++ 8-/, so i'll better ask here too.. CStringW is a class in atl/mfc dealing with strings 8-]. the only typecast operator i found is to PCXSTR...
7
9320
by: jalkadir | last post by:
WinXP P3 MinWing-GCC-3.4.3 When compiling the program below, I get a message that reads: wcout' is not a member of `std' What am I doing wrong? This message popping up too often!! >:( TIA
3
4730
by: Steven T. Hatton | last post by:
There's probably something obvious I'm missing here, but I can't seem to figure out how to get this to work: ostream_iterator<wstring, wchar_t>(wcout,"\n")); When I try to compile it, I get an error telling me it doesn't like the char. wcout.widen('\n') doesn't work here because it wants a string. Is there a way to make this work? --...
1
3591
by: iwongu | last post by:
Hi, I have a question about std::wcout and its underlying C functions. According to C99, a stream have an orientation type that is one of byte-oriented or wide-one, and it is determined by the first using C function on that stream. And the specific orientation functions should not be applied to the stream that have other orientation.
2
7734
by: Ralf Goertz | last post by:
Hi, can I mix output to cout and wcout? It seems that if I write to cout first it works fine but if I start with wcout the output to cout vanishes. I assume that is has to do with the initialization of standard output, right? #include <iostream> using namespace std;
2
3882
by: year1943 | last post by:
There was the same topic not so long ago, but as I see it stays w/o answer: http://groups.google.ru/group/comp.lang.c++/browse_thread/thread/9a05d7bba9394a60/fe109c899f916871?lnk=gst&q=locale+imbue&rnum=1&hl=ru#fe109c899f916871 As Bjarne Stroustrup said in his book, "in Stroustrup (retranslated from German) "Setting the global locale does...
44
4339
by: Ioannis Vranos | last post by:
Has anyone actually managed to print non-English text by using wcout or wprintf and the rest of standard, wide character functions?
0
7888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7811
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8314
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8185
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3811
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.