473,395 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

std::string and std::wostream

Hi,

I was wondering why there was no predefine operator<< puting a
std::string into a std::wostream. The strangest is that something
similar to this would work:

std::wostream& operator<<(std::wostream& stream, std::string str)
{
stream << str.c_str();
return stream;
}

The only problem with this solution is it invalidate the different
iterators you have on the string (because of the call to c_str), while
the operator<< should not do that.

Thanks,

Pierre
Aug 11 '06 #1
3 8304
Pierre Barbier de Reuille wrote:
Hi,

I was wondering why there was no predefine operator<< puting a
std::string into a std::wostream. The strangest is that something
similar to this would work:

std::wostream& operator<<(std::wostream& stream, std::string str)
{
stream << str.c_str();
return stream;
}

The only problem with this solution is it invalidate the different
iterators you have on the string (because of the call to c_str), while
the operator<< should not do that.
No, it would not invalidate iterators: you are passing str by value.
Therefore, a copy is being made and the call to c_str() invalidates
iterators to that copy but not to the original. However, you probably
didn't mean to pass by value.

On the other hand, I think that the above implementation does not deal
properly with embedded 0 characters. To be safe, I would start from:

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>

std::wostream & operator<< ( std::wostream & ostr,
std::string const & str ) {
std::copy ( str.begin(), str.end(),
std::ostream_iterator<char, std::wchar_t>( ostr ) );
return ( ostr );
}
Best

Kai-Uwe Bux

Aug 11 '06 #2
In article <44**********************@news.free.fr>,
Pierre Barbier de Reuille <p.****************@free.frwrote:
Hi,

I was wondering why there was no predefine operator<< puting a
std::string into a std::wostream. The strangest is that something
similar to this would work:

std::wostream& operator<<(std::wostream& stream, std::string str)
{
stream << str.c_str();
return stream;
}

The only problem with this solution is it invalidate the different
iterators you have on the string (because of the call to c_str), while
the operator<< should not do that.
Calling c_str() on a string doesn't invalidate any iterators that may
point into the string.
Aug 11 '06 #3
Daniel T. wrote:
In article <44**********************@news.free.fr>,
Pierre Barbier de Reuille <p.****************@free.frwrote:
>Hi,

I was wondering why there was no predefine operator<< puting a
std::string into a std::wostream. The strangest is that something
similar to this would work:

std::wostream& operator<<(std::wostream& stream, std::string str)
{
stream << str.c_str();
return stream;
}

The only problem with this solution is it invalidate the different
iterators you have on the string (because of the call to c_str), while
the operator<< should not do that.

Calling c_str() on a string doesn't invalidate any iterators that may
point into the string.
Sadly enough it may ! See section 23.5.2 §5 in the norm for reference:

"""
References, pointers, and iterators referring to the elements of a
basic_string sequence may be invalidated by the following uses of that
basic_string object:
— As an argument to non-member functions swap() (21.3.7.8), operator>>()
(21.3.7.9), and getline() (21.3.7.9).
— As an argument to basic_string::swap().
— Calling data() and c_str() member functions.
— Calling non-const member functions, except operator[](), at(),
begin(), rbegin(), end(), and rend().
— Subsequent to any of the above uses except the forms of insert() and
erase() which return iterators, the first call to non-const member
functions operator[](), at(), begin(), rbegin(), end(), or rend().
"""

Pierre
Aug 11 '06 #4

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

Similar topics

2
by: Rob Richardson | last post by:
Greetings! I am running into a problem with implicit conversion of arguments when trying to insert the contents of a CString object into a stringstream. I am using the following typedef: ...
32
by: len v | last post by:
A recent (Oct 3) Fox Trox comic (Bill Amend ) got me thinking causing me to edit the origional comic. Bill then had to write a patch, as most C programers must do.(...
2
by: Siegfried Heintze | last post by:
How do I use a template to write these friend functions once? The are nearly identical. Also, why does wcout << L'A'; print 65 instead of a character? Thanks, Siegfried struct DVDDeltaTime{ //...
2
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...
21
by: Alf P. Steinbach | last post by:
Just because there seems to be a lack of post-standard _correct_ tutorials: <url: http://home.no.net/dubjai/win32cpptut/>. Disclaimer: written this evening so perhaps there are "bugs" in the...
1
by: Vijai Kalyan | last post by:
I am not sure if this OT, but I will go ahead and post. If it has to be posted elsewhere let me know. -vijai. ------ I have a class declared as follows: #pragma once // a simple reference...
4
by: blackswift | last post by:
Hello,all I hava a problem that when did cout call its destructor? I used GCC compiler under linux , It compiles OK. and gives me : cons des as I haved expected.
10
by: Erik Knudsen | last post by:
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:...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.