Re: I'm trying to recreate printf
On Mon, 03 Sep 2007 03:58:46 -0000, alexl <alextheblade@gmail.com> wrote:
thanks, it worked!
for some reason wcout still needs the cast
CStringW s;
va_list args;
va_start (args,szFormat);
s.FormatV(szFormat,args);
va_end (args);
wcout << static_cast<const wchar_t*>(s) ;
Yes, you do need the cast, because the C++ Standard doesn't define an
explicit operator<< for const wchar_t* like it does for const char*, and
template argument deduction doesn't consider conversion operators such as
CStringW's operator LPCWSTR(). Thus, when you don't use the cast, you get
the member operator<<(const void*), which can and does use the conversion
operator.
Alternatively, and preferably, since the code does compile when you omit
the cast but produces wrong output, you can define a suitable inserter for
CStringW, e.g.
std::wostream& operator<<(std::wostream& os, const CStringW& s)
{
return os << static_cast<const wchar_t*>(s);
}
(Thank you to Giovanni for private communication on the absence of an
explicit const wchar_t* inserter.)
--
Doug Harrison
Visual C++ MVP