M
Martin T.
Hello.
I tried to overload the operator<< for implicit printing of wchar_t
string on a char stream.
Normally using it on a ostream will succeed as
However, I am using the boost::format library that internally makes use
of a string stream to store the formatted string. There:
std::basic_ostream<char,std::char_traits<char> >:perator<<(___)
will always be called.
On what basis will the compiler choose which kind of operator<< will be
called??
(see test code below)
thanks.
br,
Martin
test code:
###############
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
// Output wchar_t* strings on char streams instead of formatting as
void* ...
ostream& operator<<(ostream& os, const wchar_t* c_ptr) {
return os << "(TYPE:wchar_t*)"; // placeholder
}
// Boost.Format library
#include <boost/format.hpp>
////////////////////////////////////////
int main(int argc, char* argv[])
{
ostringstream ostr;
ostream& o = ostr;
o << "\nTest of ostream << operator ..." << endl;
wchar_t* wstr = L"Hello world!";
o << "Output of wchar_t* -- " << wstr << endl;
// CALL STACK ...
// (1)
opov.exe!operator<<(std::basic_ostream<char,std::char_traits<char> > &
os={...}, const wchar_t * c_ptr=0x0042f8f0) Line 6 + 0xe bytes C++
// (0) opov.exe!main(int argc=1, char * * argv=0x00356828) Line 17 +
0x26 bytes C++
o << "\nTest of boost-library ostream << operator ..." << endl;
o << "Boost formatted wchar_t* -- " << boost::str(boost::format("%1%")
% wstr) << endl;
// CALL STACK ...
opov.exe!boost::io::detail:ut_last<char,std::char_traits<char>,wchar_t
*>(std::basic_ostream<char,std::char_traits<char> > & os={...}, wchar_t
* const & x=0x00433a54) Line 98 + 0x11 bytes C++
// (4)
opov.exe!boost::io::detail:ut<char,std::char_traits<char>,std::allocator<char>,wchar_t
* const &>(wchar_t * const & x=0x00433a54, const
res="",
opov.exe!boost::io::detail::distribute<char,std::char_traits<char>,std::allocator<char>,wchar_t
* const
opov.exe!boost::io::detail::feed<char,std::char_traits<char>,std::allocator<char>,wchar_t
* const
// (0) opov.exe!main(int argc=1, char * * argv=0x00356828) Line 53 C++
// As can be seen on (5) the parameter (now called x) is of type wchar_t*
// However, the basic_ostream operator<<(void*) will be called.
cout << ostr.str();
}
##################
I tried to overload the operator<< for implicit printing of wchar_t
string on a char stream.
Normally using it on a ostream will succeed as
will be called.std::operator said:>(std::basic_ostream<char,std::char_traits<char> > & _Ostr={...}, ___)
However, I am using the boost::format library that internally makes use
of a string stream to store the formatted string. There:
std::basic_ostream<char,std::char_traits<char> >:perator<<(___)
will always be called.
On what basis will the compiler choose which kind of operator<< will be
called??
(see test code below)
thanks.
br,
Martin
test code:
###############
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
// Output wchar_t* strings on char streams instead of formatting as
void* ...
ostream& operator<<(ostream& os, const wchar_t* c_ptr) {
return os << "(TYPE:wchar_t*)"; // placeholder
}
// Boost.Format library
#include <boost/format.hpp>
////////////////////////////////////////
int main(int argc, char* argv[])
{
ostringstream ostr;
ostream& o = ostr;
o << "\nTest of ostream << operator ..." << endl;
wchar_t* wstr = L"Hello world!";
o << "Output of wchar_t* -- " << wstr << endl;
// CALL STACK ...
char * _Val=0x0042f860) Line 747 C++// (2) opov.exe!std::operator said:>(std::basic_ostream<char,std::char_traits<char> > & _Ostr={...}, const
// (1)
opov.exe!operator<<(std::basic_ostream<char,std::char_traits<char> > &
os={...}, const wchar_t * c_ptr=0x0042f8f0) Line 6 + 0xe bytes C++
// (0) opov.exe!main(int argc=1, char * * argv=0x00356828) Line 17 +
0x26 bytes C++
o << "\nTest of boost-library ostream << operator ..." << endl;
o << "Boost formatted wchar_t* -- " << boost::str(boost::format("%1%")
% wstr) << endl;
// CALL STACK ...
// (5)// (6) msvcp80d.dll!std::basic_ostream said:>:perator<<(const void * _Val=0x00433a54) Line 456 C++
opov.exe!boost::io::detail:ut_last<char,std::char_traits<char>,wchar_t
*>(std::basic_ostream<char,std::char_traits<char> > & os={...}, wchar_t
* const & x=0x00433a54) Line 98 + 0x11 bytes C++
// (4)
opov.exe!boost::io::detail:ut<char,std::char_traits<char>,std::allocator<char>,wchar_t
* const &>(wchar_t * const & x=0x00433a54, const
std::basic_string<char,std::char_traits<char>,std::allocator<char> > &boost::io::detail::format_item said:> & specs={...},
res="",
// (3)boost::io::basic_altstringbuf said:> & buf={...}, std::locale * loc_p=0x00000000) Line 150 + 0x2c bytes C++
opov.exe!boost::io::detail::distribute<char,std::char_traits<char>,std::allocator<char>,wchar_t
* const
// (2)&>(boost::basic_format said:> & self={...}, wchar_t * const & x=0x00433a54) Line 242 + 0x3f bytes C++
opov.exe!boost::io::detail::feed<char,std::char_traits<char>,std::allocator<char>,wchar_t
* const
// (1)&>(boost::basic_format said:> & self={...}, wchar_t * const & x=0x00433a54) Line 251 + 0xd bytes C++
0xd bytes C++opov.exe!boost::basic_format said:>:perator%<wchar_t *>(wchar_t * const & x=0x00433a54) Line 64 +
// (0) opov.exe!main(int argc=1, char * * argv=0x00356828) Line 53 C++
// As can be seen on (5) the parameter (now called x) is of type wchar_t*
// However, the basic_ostream operator<<(void*) will be called.
cout << ostr.str();
}
##################