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
std::operator<< <std::char_trai ts<char>
will be called.
However, I am using the boost::format library that internally makes use
of a string stream to store the formatted string. There:
std::basic_ostr eam<char,std::c har_traits<char ::operator<<(__ _)
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<<(ostr eam& 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 ...
// (2) opov.exe!std::o perator<<<std:: char_traits<cha r>
char * _Val=0x0042f860 ) Line 747 C++
// (1)
opov.exe!operat or<<(std::basic _ostream<char,s td::char_traits <char &
os={...}, const wchar_t * c_ptr=0x0042f8f 0) Line 6 + 0xe bytes C++
// (0) opov.exe!main(i nt 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(boos t::format("%1%" )
% wstr) << endl;
// CALL STACK ...
// (6) msvcp80d.dll!st d::basic_ostrea m<char,std::cha r_traits<char>
// (5)
opov.exe!boost: :io::detail::pu t_last<char,std ::char_traits<c har>,wchar_t
*>(std::basic_o stream<char,std ::char_traits<c har & os={...}, wchar_t
* const & x=0x00433a54) Line 98 + 0x11 bytes C++
// (4)
opov.exe!boost: :io::detail::pu t<char,std::cha r_traits<char>, std::allocator< char>,wchar_t
* const &>(wchar_t * const & x=0x00433a54, const
boost::io::deta il::format_item <char,std::char _traits<char>,s td::allocator<c har>
std::basic_stri ng<char,std::ch ar_traits<char> ,std::allocator <char &
res="",
boost::io::basi c_altstringbuf< char,std::char_ traits<char>,st d::allocator<ch ar>
// (3)
opov.exe!boost: :io::detail::di stribute<char,s td::char_traits <char>,std::all ocator<char>,wc har_t
* const
&>(boost::basic _format<char,st d::char_traits< char>,std::allo cator<char>
// (2)
opov.exe!boost: :io::detail::fe ed<char,std::ch ar_traits<char> ,std::allocator <char>,wchar_ t
* const
&>(boost::basic _format<char,st d::char_traits< char>,std::allo cator<char>
// (1)
opov.exe!boost: :basic_format<c har,std::char_t raits<char>,std ::allocator<cha r>
0xd bytes C++
// (0) opov.exe!main(i nt 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
std::operator<< <std::char_trai ts<char>
>(std::basic_os tream<char,std: :char_traits<ch ar & _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_ostr eam<char,std::c har_traits<char ::operator<<(__ _)
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<<(ostr eam& 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 ...
// (2) opov.exe!std::o perator<<<std:: char_traits<cha r>
>(std::basic_os tream<char,std: :char_traits<ch ar & _Ostr={...}, const
// (1)
opov.exe!operat or<<(std::basic _ostream<char,s td::char_traits <char &
os={...}, const wchar_t * c_ptr=0x0042f8f 0) Line 6 + 0xe bytes C++
// (0) opov.exe!main(i nt 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(boos t::format("%1%" )
% wstr) << endl;
// CALL STACK ...
// (6) msvcp80d.dll!st d::basic_ostrea m<char,std::cha r_traits<char>
>::operator<<(c onst void * _Val=0x00433a54 ) Line 456 C++
opov.exe!boost: :io::detail::pu t_last<char,std ::char_traits<c har>,wchar_t
*>(std::basic_o stream<char,std ::char_traits<c har & os={...}, wchar_t
* const & x=0x00433a54) Line 98 + 0x11 bytes C++
// (4)
opov.exe!boost: :io::detail::pu t<char,std::cha r_traits<char>, std::allocator< char>,wchar_t
* const &>(wchar_t * const & x=0x00433a54, const
boost::io::deta il::format_item <char,std::char _traits<char>,s td::allocator<c har>
& specs={...},
res="",
boost::io::basi c_altstringbuf< char,std::char_ traits<char>,st d::allocator<ch ar>
& buf={...}, std::locale * loc_p=0x0000000 0) Line 150 + 0x2c bytes C++
opov.exe!boost: :io::detail::di stribute<char,s td::char_traits <char>,std::all ocator<char>,wc har_t
* const
&>(boost::basic _format<char,st d::char_traits< char>,std::allo cator<char>
& self={...}, wchar_t * const & x=0x00433a54) Line 242 + 0x3f bytes C++
opov.exe!boost: :io::detail::fe ed<char,std::ch ar_traits<char> ,std::allocator <char>,wchar_ t
* const
&>(boost::basic _format<char,st d::char_traits< char>,std::allo cator<char>
& self={...}, wchar_t * const & x=0x00433a54) Line 251 + 0xd bytes C++
opov.exe!boost: :basic_format<c har,std::char_t raits<char>,std ::allocator<cha r>
>::operator%<wc har_t *>(wchar_t * const & x=0x00433a54) Line 64 +
// (0) opov.exe!main(i nt 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();
}
############### ###
Comment