Re: Strange ostringstream behaviour in Visual C++ 2005



martin.s.davies@xxxxxxxxxxxxxxxxx wrote:
I have created a logger class, of which a simplified version is shown
below:

class MyLog : public std::ostringstream
{
public:
MyLog()
{
}

~MyLog()
{
std::cout << str();
}
};

It is used as follows:

MyLog() << "Hello";

In Visual C++ 6, the above line of code would have outputted 'Hello'
in a command window. However when I compile the same code in Visual C+
+ 2005, I get what output like '00465358', which appears to be the
address of the static string after the << operator.

On stepping into the code, it appears that
basic_ostream::operator<<(const void *_Val) is called, rather than
operator<<(const char*_Val), which is what I would have expected.

The problem came up here some weeks ago already, search the archives.
Basically, the problem is that 'MyLog()' yields a temporary, which can't be
bound to a non-const reference. So, for the << call, only memberfunctions
are viable candidates, and eventually the void* overload is the one that is
taken.

template<typename T>
ostream& // Note: not a MyLog!
operator<<( MyLog const& lg, T const& t)
{
return lg.target << t;
}

This then requires you to make the ostringstream a member called 'target'
and to make that member mutable.


Uli

.



Relevant Pages

  • Re: state of unicode support
    ... intermittent member of this list, not a constant member, since I first ... thread before I posted the question -- two posts that didn't help. ... strict Unicode support good, ... of archives to see if I've missed something relevant. ...
    (comp.lang.ruby)
  • RE: Couple Member HELP
    ... COUPLEXX member in parmlib to reflect the new names. ... email to listserv@xxxxxxxxxxx with the message: GET IBM-MAIN INFO Search ... the archives at http://bama.ua.edu/archives/ibm-main.html ... Confidentiality Notice ...
    (bit.listserv.ibm-main)
  • Re: EXECIO
    ... I have seen two people corrupt members using ISPF with different member names. ... send email to listserv@xxxxxxxxxxx with the message: GET IBM-MAIN INFO ... Search the archives at http://bama.ua.edu/archives/ibm-main.html ... may contain confidential and/or privileged information. ...
    (bit.listserv.ibm-main)
  • RE: HSM Missing Member from Recalled Dataset
    ... the member that he had added was missing. ... I checked through SMF and saw the activity. ... The same colleague is currently browsing the IBM-MAIN archives and found ...
    (bit.listserv.ibm-main)
  • Re: [Rexx on z/Os]Point to particular member of PDS
    ... >>Does somebody know how to read a particular member of PDS using MVS ... If you want to read a single member, allocate ... > send email to listserv@xxxxxxxxxxx with the message: GET IBM-MAIN INFO ... > Search the archives at http://bama.ua.edu/archives/ibm-main.html ...
    (bit.listserv.ibm-main)

Loading