By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,261 Members | 1,321 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,261 IT Pros & Developers. It's quick & easy.

std::toupper

P: n/a
Aside from the pitfalls of using this function,
according to the standard, what is the correct
way to call it?

#include <string>
#include <locale>
// seems to work with BCC5.6/STLPort
void stringToUpper(std::string &str) {
for(size_t i = 0, len = str.size(); i < len; ++i)
str[i] = std::toupper(str[i]);
}

// comeaux throws an error about the arg list
// but this seems to compile
void stringToUpper(std::string &str) {
for(size_t i = 0, len = str.size(); i < len; ++i)
str[i] = std::toupper(str[i], std::locale(0));
}

For MSVC6, it complains that toupper() is not
a member of std.

In each case, including <ctypes.h> and removing
the std specifier seems to compile the first example.

Which is correct?


--
....
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Duane" <spoo@zowie_flarn.com> wrote in message
news:1l*********************@news20.bellglobal.com ...
Aside from the pitfalls of using this function,
according to the standard, what is the correct
way to call it?

#include <string>
#include <locale>
// seems to work with BCC5.6/STLPort
void stringToUpper(std::string &str) {
for(size_t i = 0, len = str.size(); i < len; ++i)
str[i] = std::toupper(str[i]);
}

// comeaux throws an error about the arg list
// but this seems to compile
void stringToUpper(std::string &str) {
for(size_t i = 0, len = str.size(); i < len; ++i)
str[i] = std::toupper(str[i], std::locale(0));
}

For MSVC6, it complains that toupper() is not
a member of std.

In each case, including <ctypes.h> and removing
the std specifier seems to compile the first example.

Which is correct?


V6 does not put C library names in namespace std when it
should, due to compiler limitations. Fixed with later
versions of VC++.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #2

P: n/a

"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:bW*********************@nwrddc01.gnilink.net. ..
<snip>
V6 does not put C library names in namespace std when it
should, due to compiler limitations. Fixed with later
versions of VC++.


Thanks. We're moving from Borland to MSVC7.1 which (I
believe) uses your libraries.

I would still like to know what the correct way to call
this should be according to the standard.
From the dinkumware help I get:

template<class Elem>
Elem toupper(Elem ch, const locale &loc);
Given this, I don't really see how any of my examples
could be correct (except to use ctypes.h)

The second arg here is a const reference so BCC's
accepting toupper(char) doesn't seem correct.
But neither does Comeaux's accepting
toupper(char, std::locale(0));
as in this case, the second arg should be
a temporary.

At any rate, what does the standard say about this?
Jul 22 '05 #3

P: n/a
"Duane" <spoo@zowie_flarn.com> wrote in message
news:J3*********************@news20.bellglobal.com ...
"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:bW*********************@nwrddc01.gnilink.net. ..
<snip>
V6 does not put C library names in namespace std when it
should, due to compiler limitations. Fixed with later
versions of VC++.


Thanks. We're moving from Borland to MSVC7.1 which (I
believe) uses your libraries.

I would still like to know what the correct way to call
this should be according to the standard.
From the dinkumware help I get:

template<class Elem>
Elem toupper(Elem ch, const locale &loc);
Given this, I don't really see how any of my examples
could be correct (except to use ctypes.h)

The second arg here is a const reference so BCC's
accepting toupper(char) doesn't seem correct.
But neither does Comeaux's accepting
toupper(char, std::locale(0));
as in this case, the second arg should be
a temporary.

At any rate, what does the standard say about this?


You should be able to write std::toupper('x', std::locale()),
but that should also be the same as toupper('x'). The
default locale constructor should mirror the global locale.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #4

P: n/a

"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:CV*******************@nwrddc03.gnilink.net...
You should be able to write std::toupper('x', std::locale()),
but that should also be the same as toupper('x'). The
default locale constructor should mirror the global locale.


Thanks. I was surprised that std::toupper('x') failed
on Comeau. It seems that Borland's STLPort implementation
is broken as well as it doesn't accept the call with 2 args.
Jul 22 '05 #5

P: n/a

"Duane" <spoo@zowie_flarn.com> wrote in message
news:kc*********************@news20.bellglobal.com ...

"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:CV*******************@nwrddc03.gnilink.net...
You should be able to write std::toupper('x', std::locale()),
but that should also be the same as toupper('x'). The
default locale constructor should mirror the global locale.


Well with MSVC7.1, it acts the same as Comeau's test compiler.
It takes std::toupper('x',std::locale()); but errors on std::toupper('x');
Jul 22 '05 #6

P: n/a
"Duane" <spoo@zowie_flarn.com> wrote in message
news:Y7*********************@news20.bellglobal.com ...
"Duane" <spoo@zowie_flarn.com> wrote in message
news:kc*********************@news20.bellglobal.com ...

"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:CV*******************@nwrddc03.gnilink.net...
You should be able to write std::toupper('x', std::locale()),
but that should also be the same as toupper('x'). The
default locale constructor should mirror the global locale.


Well with MSVC7.1, it acts the same as Comeau's test compiler.
It takes std::toupper('x',std::locale()); but errors on std::toupper('x');


You're probably running afoul of a macro definition of toupper in
ctype.h. The C++ Standard says that C headers must not supply
macro overrides, but they often do anyway.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #7

P: n/a

"P.J. Plauger" <pj*@dinkumware.com> wrote in message news:sn*****************@nwrddc01.gnilink.net...
You're probably running afoul of a macro definition of toupper in
ctype.h. The C++ Standard says that C headers must not supply
macro overrides, but they often do anyway.


Possibly. Or possibly it's some of the "language extensions" that are defaulted
to on in VC. I setup a console app, turned off a lot of stuff like managed extensions,
removed the stdfax.h include, changed _tmain to int main() and then it worked as
you say. Thanks.
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.