std::toupper

D

Duane

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 = std::toupper(str);
}

// 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 = std::toupper(str, 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?
 
P

P.J. Plauger

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 = std::toupper(str);
}

// 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 = std::toupper(str, 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
 
D

Duane

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?
 
P

P.J. Plauger

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
 
D

Duane

P.J. Plauger said:
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.
 
D

Duane

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');
 
P

P.J. Plauger

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
 
D

Duane Hebert

P.J. Plauger said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top