transform algorithm and casting of functions

Discussion in 'C++' started by suresh, Dec 22, 2007.

  1. suresh

    suresh Guest

    Hi

    I saw this code in the net for converting case.

    string s = "Amma";
    transform(s.begin(),s.end(),s.begin(),(int(*)(int))toupper);

    Q: what is the meaning of (int(*)(int))toupper ?

    The code works fine when (int(*)(int))toupper is replaced
    by ::toupper. But the code does not even compile, when i write just
    std::toupper i.e.
    transform(s.begin(),s.end(),s.begin(),std::toupper); //not even
    compiling

    Could you please explain the difference between the three?

    thanks
    suresh
    suresh, Dec 22, 2007
    #1
    1. Advertising

  2. On Dec 22, 8:08 pm, suresh <> wrote:
    > Hi
    >
    > I saw this code in the net for converting case.
    >
    > string s = "Amma";
    > transform(s.begin(),s.end(),s.begin(),(int(*)(int))toupper);
    >
    > Q: what is the meaning of  (int(*)(int))toupper ?
    >


    It means it is casting pointer to function toupper as a pointer to a
    function having return type as int and takes one int argument. I think
    that is not required as toupper is of the same function pointer type.

    > The code works fine when (int(*)(int))toupper is replaced
    > by ::toupper. But the code does not even compile, when i write just
    > std::toupper i.e.
    > transform(s.begin(),s.end(),s.begin(),std::toupper); //not even
    > compiling
    >
    > Could you please explain the difference between the three?


    I think you are using VC++ 6.0 (or something as old/non-conforming as
    that). It does not put the C APIs into std namespace (they are in
    global namespace). Get rid of it and std::toupper works.
    Abhishek Padmanabh, Dec 22, 2007
    #2
    1. Advertising

  3. suresh

    suresh Guest

    Thanks abhishek for the comments. But I am using g++ on a debian etch
    machine. It is not VC++ and std::toupper is not compiling...

    any suggestions?

    suresh

    On Dec 22, 10:34 pm, Abhishek Padmanabh <>
    wrote:
    > On Dec 22, 8:08 pm,suresh<> wrote:
    >
    > > Hi

    >
    > > I saw this code in the net for converting case.

    >
    > > string s = "Amma";
    > > transform(s.begin(),s.end(),s.begin(),(int(*)(int))toupper);

    >
    > > Q: what is the meaning of (int(*)(int))toupper ?

    >
    > It means it is casting pointer to function toupper as a pointer to a
    > function having return type as int and takes one int argument. I think
    > that is not required as toupper is of the same function pointer type.
    >
    > > The code works fine when (int(*)(int))toupper is replaced
    > > by ::toupper. But the code does not even compile, when i write just
    > > std::toupper i.e.
    > > transform(s.begin(),s.end(),s.begin(),std::toupper); //not even
    > > compiling

    >
    > > Could you please explain the difference between the three?

    >
    > I think you are using VC++ 6.0 (or something as old/non-conforming as
    > that). It does not put the C APIs into std namespace (they are in
    > global namespace). Get rid of it and std::toupper works.
    suresh, Dec 30, 2007
    #3
    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. Steffen Brinkmann

    <algorithm> transform modification

    Steffen Brinkmann, Sep 21, 2003, in forum: C++
    Replies:
    20
    Views:
    785
    Steffen Brinkmann
    Sep 24, 2003
  2. Patrick Guio

    STL transform algorithm

    Patrick Guio, Nov 2, 2005, in forum: C++
    Replies:
    9
    Views:
    535
    Clark S. Cox III
    Nov 2, 2005
  3. Piotr

    STL transform algorithm

    Piotr, Feb 14, 2006, in forum: C++
    Replies:
    5
    Views:
    439
    Daniel T.
    Feb 14, 2006
  4. Diego Martins

    Transform algorithm 1-n, n-1 or m-n

    Diego Martins, Oct 18, 2006, in forum: C++
    Replies:
    2
    Views:
    292
    Fraser Ross
    Oct 19, 2006
  5. Gerald I. Evenden

    Problem with <algorithm> transform

    Gerald I. Evenden, Jan 6, 2008, in forum: C++
    Replies:
    13
    Views:
    549
    James Kanze
    Jan 8, 2008
Loading...

Share This Page