std::toupper

Discussion in 'C++' started by Duane, May 14, 2004.

  1. Duane

    Duane Guest

    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?




    --
    ....
    Duane, May 14, 2004
    #1
    1. Advertising

  2. Duane

    P.J. Plauger Guest

    "Duane" <spoo@zowie_flarn.com> wrote in message
    news:1l3pc.78352$...

    > 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
    P.J. Plauger, May 14, 2004
    #2
    1. Advertising

  3. Duane

    Duane Guest

    "P.J. Plauger" <> wrote in message
    news:bW3pc.194725$...
    <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?
    Duane, May 14, 2004
    #3
  4. Duane

    P.J. Plauger Guest

    "Duane" <spoo@zowie_flarn.com> wrote in message
    news:J34pc.78413$...

    > "P.J. Plauger" <> wrote in message
    > news:bW3pc.194725$...
    > <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
    P.J. Plauger, May 14, 2004
    #4
  5. Duane

    Duane Guest

    "P.J. Plauger" <> wrote in message
    news:CV5pc.71605$...
    > 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.
    Duane, May 14, 2004
    #5
  6. Duane

    Duane Guest

    "Duane" <spoo@zowie_flarn.com> wrote in message
    news:kc7pc.79583$...
    >
    > "P.J. Plauger" <> wrote in message
    > news:CV5pc.71605$...
    > > 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');
    Duane, May 21, 2004
    #6
  7. Duane

    P.J. Plauger Guest

    "Duane" <spoo@zowie_flarn.com> wrote in message
    news:Y7urc.89355$...

    > "Duane" <spoo@zowie_flarn.com> wrote in message
    > news:kc7pc.79583$...
    > >
    > > "P.J. Plauger" <> wrote in message
    > > news:CV5pc.71605$...
    > > > 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
    P.J. Plauger, May 22, 2004
    #7
  8. Duane

    Duane Hebert Guest

    "P.J. Plauger" <> wrote in message news:snxrc.1463$...
    > 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.
    Duane Hebert, May 22, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri

    String.ToUpper

    Kerri, Oct 26, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,054
    Chris Botha
    Oct 27, 2003
  2. ben

    toupper wont work

    ben, Feb 21, 2004, in forum: C++
    Replies:
    5
    Views:
    3,129
    Dietmar Kuehl
    Feb 25, 2004
  3. JasBascom

    toupper

    JasBascom, Feb 24, 2004, in forum: C++
    Replies:
    2
    Views:
    657
    Mike Wahler
    Feb 24, 2004
  4. Randy Rieger

    Newbie - about using toupper/tolower

    Randy Rieger, Jul 7, 2003, in forum: C Programming
    Replies:
    3
    Views:
    1,545
    Randy Rieger
    Jul 7, 2003
  5. didgerman

    structure toupper\lower?

    didgerman, Jan 16, 2004, in forum: C Programming
    Replies:
    18
    Views:
    769
    didgerman
    Jan 17, 2004
Loading...

Share This Page