473,804 Members | 3,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8368
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_it erator<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.frwro te:
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.frwro te:
>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::wostrea m& 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::s wap().
— 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
13756
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: typedef std::basic_stringstream<TCHAR> ustringstream; I have a function that somewhat resembles the following:
32
1863
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.( http://homepage.mac.com/billamend/images/patch.gif ) To see the comparison, go to ---> http://perl.hacker.freeservers.com/
2
2066
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{ // constructors go here... friend std::ostream & operator<<(std::ostream &os, const DVDDeltaTime& dt ){ return os
2
3077
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 that is LPCWSTR that is simply "const unsigned short*" (wchar_t is unsigned short). wcout accepts...
21
4119
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 presentation -- are there? Plea: if someone takes the time to convert the word document to clean xhtml perhaps with stylish clear readable layout, then it will be an incentive for me to go on to write a next part, and a next part...
1
6267
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 counter implementation
4
2289
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
3416
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: ------------------------------ const char *lookup() { return "Hello world";
158
7915
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 discouraged due to some specific reason. If someone can give inputs on the same, it will be of great help. Regards, Pushpa
0
9706
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9584
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10337
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2995
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.