Connecting Tech Pros Worldwide Forums | Help | Site Map

std::toupper

Duane
Guest
 
Posts: n/a
#1: Jul 22 '05
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?




--
....



P.J. Plauger
Guest
 
Posts: n/a
#2: Jul 22 '05

re: std::toupper


"Duane" <spoo@zowie_flarn.com> wrote in message
news:1l3pc.78352$FH5.1733044@news20.bellglobal.com ...
[color=blue]
> 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?[/color]

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


Duane
Guest
 
Posts: n/a
#3: Jul 22 '05

re: std::toupper



"P.J. Plauger" <pjp@dinkumware.com> wrote in message
news:bW3pc.194725$L31.120616@nwrddc01.gnilink.net. ..
<snip>[color=blue]
> V6 does not put C library names in namespace std when it
> should, due to compiler limitations. Fixed with later
> versions of VC++.[/color]

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.J. Plauger
Guest
 
Posts: n/a
#4: Jul 22 '05

re: std::toupper


"Duane" <spoo@zowie_flarn.com> wrote in message
news:J34pc.78413$FH5.1747411@news20.bellglobal.com ...
[color=blue]
> "P.J. Plauger" <pjp@dinkumware.com> wrote in message
> news:bW3pc.194725$L31.120616@nwrddc01.gnilink.net. ..
> <snip>[color=green]
> > V6 does not put C library names in namespace std when it
> > should, due to compiler limitations. Fixed with later
> > versions of VC++.[/color]
>
> 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?[/color]

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


Duane
Guest
 
Posts: n/a
#5: Jul 22 '05

re: std::toupper



"P.J. Plauger" <pjp@dinkumware.com> wrote in message
news:CV5pc.71605$sK3.37782@nwrddc03.gnilink.net...[color=blue]
> 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.[/color]

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.


Duane
Guest
 
Posts: n/a
#6: Jul 22 '05

re: std::toupper



"Duane" <spoo@zowie_flarn.com> wrote in message
news:kc7pc.79583$FH5.1818565@news20.bellglobal.com ...[color=blue]
>
> "P.J. Plauger" <pjp@dinkumware.com> wrote in message
> news:CV5pc.71605$sK3.37782@nwrddc03.gnilink.net...[color=green]
> > 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.[/color][/color]

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.J. Plauger
Guest
 
Posts: n/a
#7: Jul 22 '05

re: std::toupper


"Duane" <spoo@zowie_flarn.com> wrote in message
news:Y7urc.89355$325.1962303@news20.bellglobal.com ...
[color=blue]
> "Duane" <spoo@zowie_flarn.com> wrote in message
> news:kc7pc.79583$FH5.1818565@news20.bellglobal.com ...[color=green]
> >
> > "P.J. Plauger" <pjp@dinkumware.com> wrote in message
> > news:CV5pc.71605$sK3.37782@nwrddc03.gnilink.net...[color=darkred]
> > > 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.[/color][/color]
>
> 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');[/color]

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


Duane Hebert
Guest
 
Posts: n/a
#8: Jul 22 '05

re: std::toupper



"P.J. Plauger" <pjp@dinkumware.com> wrote in message news:snxrc.1463$o97.1056@nwrddc01.gnilink.net...[color=blue]
> 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.[/color]

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.


Closed Thread


Similar C / C++ bytes