|
|
Type safety and C++ streams
Would it be better if this produced a compile-time error?
std::wcout << L"I am " << 31 << " years old." << std::endl;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
spam
|
6/22/2004 5:49:48 PM |
|
Daniel wrote:
>Would it be better if this produced a compile-time error?
>
>std::wcout << L"I am " << 31 << " years old." << std::endl;
Personally I would prefer it if wcout treated it correctly.
std::basic_ostream<wchar_t>&
operator<<(std::basic_ostream<wchar_t>& os, const char* c_str)
{
std::string str(c_str);
return os << std::wstring(str.begin(), str.end());
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Motti
|
6/23/2004 9:39:32 AM
|
|
Good morning Daniel,
Daniel schrieb:
>Would it be better if this produced a compile-time error?
>
>std::wcout << L"I am " << 31 << " years old." << std::endl;
>
>
Why? Should the compiler recognize, that this program is incomplete or
that the
output is not true ;-)
Honestly, what are you criticising? The fact, that there exist the
following overloads
of operator<<:
template<class charT, class traits>
std::basic_ostream<charT,traits>&
operator<<(std::basic_ostream<charT,traits>&,
const charT*);
template<class charT, class traits>
std::basic_ostream<charT,traits>&
operator<<(std::basic_ostream<charT,traits>&,
const char*);
template<class traits>
std::basic_ostream<char,traits>&
operator<<(std::basic_ostream<char,traits>&,
const char*);
as well as those for const unsigned char* and const signed char*??
Greetings from Bremen,
Daniel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
ISO
|
6/23/2004 8:29:50 PM
|
|
Motti Lanzkron <mlanzkron@yahoo.co.uk> wrote:
> Daniel wrote:
> >Would it be better if this produced a compile-time error?
> >
> >std::wcout << L"I am " << 31 << " years old." << std::endl;
>
> Personally I would prefer it if wcout treated it correctly.
Define "correctly" for this case! What IOStreams actually is they
effectively use the ctype's 'widen()' function to turn a 'char' into the
stream's character type. I would call this "more correct" than your
proposal:
> std::basic_ostream<wchar_t>&
> operator<<(std::basic_ostream<wchar_t>& os, const char* c_str)
> {
> std::string str(c_str);
> return os << std::wstring(str.begin(), str.end());
> }
.... which is just the behavior when using the "C" locale except that the
actual stream implementation omits the potentially expensive construction
of 'std::basic_string<cT>' objects: these might be required to allocate
memory which is not really necessary.
The interesting aspect of this operation is that the standard specifies
that 'widen()' is to be used for output but does not mention 'narrow()'
for the reverse operation of reading a string for a different character
type. This may be an underspecification.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
dietmar_kuehl
|
6/24/2004 11:41:49 AM
|
|
Motti Lanzkron <mlanzkron@yahoo.co.uk> wrote in message news:<2s6id09rb8i595hf3f020lipntlemgkhk0@4ax.com>...
> Daniel wrote:
> >Would it be better if this produced a compile-time error?
> >
> >std::wcout << L"I am " << 31 << " years old." << std::endl;
>
> Personally I would prefer it if wcout treated it correctly.
>
> std::basic_ostream<wchar_t>&
> operator<<(std::basic_ostream<wchar_t>& os, const char* c_str)
> {
> std::string str(c_str);
> return os << std::wstring(str.begin(), str.end());
> }
It would be an improvement if it treated it correctly - but how does
the above code snippet relate to this? ;) (It assumes that the numeric
value of each char maps onto the same numeric value of each wchar_t,
which is only true if wchar_t is Unicode and char is unsigned and
contains ISO 8859-1.)
The output of the above program on my system is:
I am 31004150BC
That is, in its efforts to provide a conversion for everything, it
treats char * the same as void * and writes out the hex value.
I can see why I might want this sometimes, but I'd estimate that 9999
times out of 10000, it would be a type error that would go straight
past the language's type system.
I can also see how this is probably very difficult if not impossible
to do anything about now, except by avoiding direct use of iostreams.
So all we can say is that the iostream interface is maybe not a good
thing to imitate in our own programs.
Its main value is in demonstrating the ability of C++ to mimic a
scripting language (automatic type conversion, chosen for you), and so
it is not so capable of detecting when you've made a type error.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
spam
|
6/25/2004 12:12:52 AM
|
|
|
4 Replies
81 Views
(page loaded in 0.081 seconds)
|
|
|
|
|
|
|
|
|