Re: Strange ostringstream behaviour in Visual C++ 2005
- From: Ulrich Eckhardt <eckhardt@xxxxxxxxxxxxxx>
- Date: Tue, 13 Mar 2007 11:55:48 +0100
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
.
- References:
- Strange ostringstream behaviour in Visual C++ 2005
- From: martin . s . davies
- Strange ostringstream behaviour in Visual C++ 2005
- Prev by Date: Strange ostringstream behaviour in Visual C++ 2005
- Next by Date: Re: map.find doesn't find
- Previous by thread: Strange ostringstream behaviour in Visual C++ 2005
- Index(es):
Relevant Pages
|
Loading