|
Boost : |
From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-02-05 06:53:24
>From: "Paul A. Bristow" <boost_at_[hidden]>
> This looks really neat - and potentially very useful.
Thanks. :)
> Sadly, array is one of the most interesting cases - so I'm sure I won't be
the
> only one 'watching this space'.
I've looked more into it, and it seems this is a problem not just for MSVC,
but a problem with the current way of doing it, overloading operator<< for
T(&)[N], sinch string literals has the type "const char[N]", as well...
With overloaded operator<< for T(&)[N], and using "std::cout << "Test" ",
g++ gives an ambiguity error, while on Intel C++, it selects the composite
overload, resulting in funny output: It tries to output the null-terminator,
as well, not knowing that for character strings, this is a string
terminator. Besides, it can't really know if the user meant to send a
character string, or a non-null-terminated character array.
It seems the differences is down to subtle implementation differences. Here
are the results, when an overloaded operator<< with T[N] is defined (S -
string-literal operator, C - composite operator (T(&)[N]), A - ambiguous):
const int array[3];
std::cout << array;
MSVC 7 - C
Intel C++ 7 - C
g++ 3.2 - C
const char array[3];
std::cout << array;
MSVC 7 - A (S or C)
Intel C++ 7 - C
g++ 3.2 - C
std::cout << "Test";
MSVC 7 - A (S or C)
Intel 7 - C
g++ 3.2 - A (S or C)
In order to allow string literals to be used, without ambiguity or the wrong
operator being selected, it seems that one needs to overload operator<< on
something else than T(&)[N]. T[N] isn't an option, either, as this is the
same as overloading on T *, which means it isn't able to deduce N.
This quandary may be solved by wrapping arrays in an object, which then is
passed to the overloaded operator<<. For example:
std::cout << wrap_array(array);
Comments/suggestions?
> PS composite_format is a bit long, but I can't suggest better.
I know, but I don't know any better, either. I see that Volodya suggests
composite_io in another posting. That's another possibility. Anyway, I guess
what's most important is to get the functionality in place. A default system
may make it easier, as well.
Regards,
Terje
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk