Writing unsigned char to std::ostream
In my MFC project I have a piece of code like
void XMLHelper::WriteHeader(std::ostream& ostrm)
{
ostrm.put(unsigned char(0xEF)); // 1
ostrm.put(unsigned char(0xBB));
ostrm.put(unsigned char(0xBF));
ostrm << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
}
where (if it matters) ostrm is opened in text mode. This code compiles
and (I think) runs as I intended on VC6-VC9.
However, I have been playing around with VMWare and Linux (Ubuntu) and
got it into my head to compile the non-GUI classes in my app using g++
(version 4.1.2). It won't compile.
I now see that my code was wrong (undefined behavior), because it
requires conversion of unsigned char to char, which is
implementation-defined (I just learned).
But this is not why it fails to compile on g++. It fails due to the use
of unsigned char as a type in this context. Both of the following
compile correctly (not sure about running):
ostrm.put(unsigned(0xEF)); // 2
ostrm.put((unsigned char)0xEF); // 3
The same thing happens on Comeau (1 fails, 2 and 3 compile).
Questions:
1. Is this a parsing bug in g++/Comeau?
2. Do you think the following is correct and portable?
void XMLHelper::WriteHeader(std::ostream& ostrm)
{
ostrm << static_cast<unsigned char>(0xEFU);
ostrm << static_cast<unsigned char>(0xBBU);
ostrm << static_cast<unsigned char>(0xBFU);
ostrm << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
}
Thanks.
--
David Wilkinson
Visual C++ MVP