On Jan 6, 10:17 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-01-06 21:45, Andrew Koenig wrote:
transform(s.beg in(), s.end(),s.begin (), toupper);
Something seems seriously wrong but I can't figure it.
Alas, toupper is a macro so you can't pass it as an argument.
Are you sure? In C99 it is a function specified as int toupper(int c)
(section 7.4.2.2, "The toupper function") and in C++98 table 45 it is
also listed as a function.
To the OP: the code you posted compiled and ran fine with
MSVC++ so I am not sure why it did not work for you.
It's undefined behavior, so it might compile. I'd be very
surprised if it worked correctly with VC++, however (unless you
compiled with the /J option).
However there is also a toupper() function in C++ declared in
the <localeheader which might work (i.e. replace <cctype>
with <localeand try again).
There are two separate issues involved here. The first is that
in C++, toupper is overloaded, with one of the overloads (the
one in <localebeing a function template. And that the
standard allows any standard C++ header to include any other.
Obviously, if the template function is visible, the compiler
can't possibly resolve type deduction for std::transform unless
you somehow tell it which overload you want to use: it's using
the type of the argument (i.e. the name of the function) to
deduce the template type of std::transfor, and it needs to know
the target type to resolve the overload on the name. If
<localehasn't been included (directly or indirectly), on the
other hand, the only function visible is the one in <cctype>,
type deduction works, and the code compiles.
The second problem is that if char is signed (which it is by
default in VC++), there is no overload of std::toupper which can
be legally called with just a single char argument: the function
template in <localerequir es two arguments, and the function in
<cctypetakes an int in the range [0...UCHAR_MAX] as an
argument---if char is signed, it can (and often will) have a
negative value, which results in undefined behavior if it is
passed to the toupper function in <ctype>. This error is so
common that a number of implementations today actually make the
code work anyway. For all values except EOF (which is almost
always -1). (If you're using ISO 8859-1---one of the more
widespread single byte encoding---then -1 from a char would
correspond to a 'ÿ', a latin small letter y with diaeresis. And
ISO 8859-1 doesn't contain a latin capital letter y with
diaeresis, so returning the same value is correct.)
Finally, of course, you might even have to ask if any of the
standard toupper functions are applicable. There is not,
generally, a one to one mapping of lower to upper, and some
lower case characters might map to a two character sequence in
upper case (German 'ß' becomes "SS", or in some special
contexts, "SZ"; Swiss German 'ä' becomes either "Ae" or "AE",
etc.). And of course, toupper (in all its forms) is totally
useless if you have a multibyte encoding, like UTF-8 (which is
the default, I believe, in most modern Linux distributions).
Depending on the application, such issues may or may not be
relevant.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34