考慮VS2010 SP1,我發現,在<ostream>
頭有這種過載std::ostringstream
和const char*
:
template<class _Traits> inline
basic_ostream<char, _Traits>& operator<<(
basic_ostream<char, _Traits>& _Ostr,
const char *_Val)
{ // insert NTBS into char stream
...
,但似乎並沒有成爲std::wostringstream
和const wchar_t*
類似的過載。
如果你把它添加到你的源代碼,發送CStringW
與operator<<
似乎工作(我的個人偏好:使用CString::GetString()
法字符串流和operator<<
):
namespace std {
template<class _Traits> inline
basic_ostream<wchar_t, _Traits>& operator<<(
basic_ostream<wchar_t, _Traits>& _Ostr,
const wchar_t *_Val)
{
ATLTRACE("It's me, the new overload!\n");
typedef wchar_t _Elem;
//
// *** Copy and paste *** the source code from the following overload:
//
// template<class _Elem,
// class _Traits> inline
// basic_ostream<_Elem, _Traits>& operator<<(
// basic_ostream<_Elem, _Traits>& _Ostr, const _Elem *_Val)
//
//
// NOTE: I don't want to infringe any copyright.
//
// Moderators please delete the following lines if they
// infringe any copyright.
//
typedef basic_ostream<_Elem, _Traits> _Myos;
ios_base::iostate _State = ios_base::goodbit;
streamsize _Count = (streamsize)_Traits::length(_Val); // may overflow
streamsize _Pad = _Ostr.width() <= 0 || _Ostr.width() <= _Count
? 0 : _Ostr.width() - _Count;
const typename _Myos::sentry _Ok(_Ostr);
if (!_Ok)
_State |= ios_base::badbit;
else
{ // state okay, insert
_TRY_IO_BEGIN
if ((_Ostr.flags() & ios_base::adjustfield) != ios_base::left)
for (; 0 < _Pad; --_Pad) // pad on left
if (_Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(_Ostr.fill())))
{ // insertion failed, quit
_State |= ios_base::badbit;
break;
}
if (_State == ios_base::goodbit
&& _Ostr.rdbuf()->sputn(_Val, _Count) != _Count)
_State |= ios_base::badbit;
if (_State == ios_base::goodbit)
for (; 0 < _Pad; --_Pad) // pad on right
if (_Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(_Ostr.fill())))
{ // insertion failed, quit
_State |= ios_base::badbit;
break;
}
_Ostr.width(0);
_CATCH_IO_(_Ostr)
}
_Ostr.setstate(_State);
return (_Ostr);
}
} // namespace std
對於運算符<<的char版本,你寫的東西是否同樣真實? – criddell
通常它也適用於'char *'版本,但我相信在''char *''的MFC深處某處定義了'operator <<'的非標準非模板版本。 –
感謝您的回覆。你寫的所有內容都是正確的,並且通過@Schepurin和Mr.C64添加的信息,我已經能夠讓我的代碼正常工作。我希望我可以選擇所有三個答案爲正確的。 – criddell