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