Strange behavoiur of ostringstream (VS.NET 2003 and C++ at all)

Tech-Archive recommends: Fix windows errors by optimizing your registry



   Hi,

   Here is my sample. It is very simple console Win32 C++ project
   but same behaviour is reproducible in other contexts.
----    stdafx.h :
#pragma once



#include <iostream>

#include <tchar.h>

----    sformat.cpp :
#include "stdafx.h"

#include <sstream>



#define SFORMAT(e) ((dynamic_cast<const std::ostringstream&>(std::ostringstream() << e)).str())

// you have to turn on /GR (rtti) for dynamic_cast.



int _tmain(int argc, _TCHAR* argv[])

{

   std::cout << SFORMAT("2 x " << " 2 = " << 2*2);

   return 0;

}

----
The strangeness is that first string constant ("2 x ") is output by method
_Myt& basic_ostream<_Elem, _Traits>::operator<<(const void *_Val)


   (header "ostream", line 414),

and the second one (" 2 = ") - by out-of-class template inserter function

basic_ostream<_Elem, _Traits>& operator<<(basic_ostream<_Elem, _Traits>& _Ostr, const char *_Val)

   (header "ostream", line 612)



Anybody knows why C++ compiler decides to lookup operators << in such way,

and how to work around this problem ? Btw, I just have reproduced the same

   behaviour under Linux/g++ 3.3.5 as well as 4.0.2, with minimal changes

   to the source (_TCHAR->char, _tmain -> main, and removed stdafx.h).



   Something strange is happening in C++ world.



   Thank you in advance for your replies.



.