473,388 Members | 1,574 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,388 software developers and data experts.

Stringstreams and CStrings

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:

void DoSomething(CString TitleString)
{
TryToDoSomething();
if (ItDidntWork())
{
ustringstream theStream;
theStream << TitleString << _T("\n");
theStream << WhyItDidntWork();
TellTheUserItDidntWork(theStream);
}
}

If _UNICODE is not defined, TCHAR evaluates to char, and everybody's
happy. The stream is built as expected. But if _UNICODE is defined,
TCHAR becomes unsigned short, and instead of the expected title, the
stream contains a memory address in hexadecimal format.

Stepping into the code at the point of the first insertion, I see that
the first thing that happens is that CString's operator(LPCTSTR)
conversion method is called. Then basic_ostream<>'s operator<<(const
void*) method is called, which just puts the pointer's value into the
stream.

If I explictly cast TitleString to an LPCTSTR, the title is inserted
into the string as expected! But an explicit cast is really ugly, and
there's a lot of places where I'd have to do it. In an attempt to
find a better way, I wrote the following function:

uostream& operator<<(uostream& theStream, const CString& theString)
{
return operator<<(theStream, (LPCTSTR)theString);
}

But when this function was executed, the const char* that I got from
the cast was converted back into a CString and I ended up in an
endless loop and a stack overflow.

Can anybody explain what is going on here and what the best way out of
this morass is?

Thanks very much!

Rob Richardson
Jul 19 '05 #1
2 13655
rr*********@pressco.com (Rob Richardson) wrote in message news:<fe*************************@posting.google.c om>...
[snip]
If _UNICODE is not defined, TCHAR evaluates to char, and everybody's
happy. The stream is built as expected. But if _UNICODE is defined,
TCHAR becomes unsigned short, and instead of the expected title, the
stream contains a memory address in hexadecimal format.

Stepping into the code at the point of the first insertion, I see that
the first thing that happens is that CString's operator(LPCTSTR)
conversion method is called. Then basic_ostream<>'s operator<<(const
void*) method is called, which just puts the pointer's value into the
stream.


I encountered this very problem a few days ago. I think that what
happens is the following. The CString is converted to (const wchar_t*)
and then there are two possible operator<< overloads to call:
std::wostream & operator<<(std::wostream &os, const wchar_t *ws);
std::wostream & std::wostream::operator<<(void *p);
(well, actually the template versions of these functions, with wchar_t
and so on)

That is, a member function that writes a pointer and a nonmember
function that writes a wide string. But now, when you write:
wchar_t *x;
os << x;
which one is selected? Well, in MSVC6 it depends on the type of the
dynamic stream: if the stream is of type the base type (std::wostream)
the member function is selected (wrong), but if the stream is of a
derived type, say wstringstream, then the nonmember function is called
(right!).
I'm not sure, but I think that this is not standard, and the compiler
is wrong, maybe someone out there can confirm this.

Actually, there are a few more cases in your code, but the idea is the
same, sometimes MSVC takes it right, sometimes it doesn't.

My solution was to write a function like the following:

uostream& operator<<(uostream& theStream, const CString& theString)
{
theStream.write(theString, theString.GetLength());
return theStream;
}

Using the write member function all should be fine. And even the cast
goes away!!

HTH
Bobo
Jul 19 '05 #2
Had a similar problem and found that the compiler for some reason didn't see
the friend operator>> functions.

using std::operator<<;

solved the problem for me but I have no idea why it is needed.
Jul 19 '05 #3

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

Similar topics

1
by: Andy | last post by:
1) Are the manipulators like std::endl and std::ends independent of character set. That is, can I use the same manipulators in both the following cases: a) <code> std::basic_stringstream<char>...
5
by: raz | last post by:
Greetings all. I apologize for what is almost certainly a stupid question, but I can't figure this out, and have no more time for head bashing... The short version: what is the appropriate...
2
by: jordan | last post by:
I am a longtime C++ programmer and I'm trying to understand how I would phrase a common idiom in Java. Consider the following program in C++, which reads tokens either from an istringstream (in...
0
by: Doug Bailey | last post by:
I have an application in which some unmanaged classes are being pulled into a Managed Windows Form application. The umanaged class has a number of CString variables. When I link the program, I...
1
by: JackHWarner | last post by:
I'm porting an app from vc++6 to vc++ 7.1, and am having some trouble exporting functions. I can export simple functions from a DLL by simply putting __declspec(dllexport) in front of them, and I...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
2
by: Sean F. Aitken | last post by:
Good afternoon, We have an app that uses a CMap with CString types for values and accepts LPCSTR as the parameter. The object being created is on the heap (created dynamically). A leak detector...
3
by: JackC | last post by:
Hi, How do i use stringstreams getline function to extract lines from an existing string? Say i have: string strlist = "line1\r\nLine2\r\nLine3\r\n"; I want to extract each line out into a...
6
by: kay kay | last post by:
#include<iostream> #include<cstring> using namespace std; void main() { char MyString="1.1 You can't have your cake and eat it". 1.2" There's light at the end of the tunnel". 1.3 "Try to...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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
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...

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.