Unresolved overload type in parameter to std::random_shuffle

Discussion in 'C++' started by Juha Nieminen, Nov 30, 2011.

  1. Any suggestions how to make this compile?

    //-----------------------------------------------------------------------
    #include <algorithm>
    #include <cstdlib>

    // The actual rng replaced with std::rand() for simplicity
    unsigned randValue() { return std::rand(); }
    unsigned randValue(unsigned modulo) { return std::rand() % modulo; }

    int main()
    {
    int table[10];
    std::random_shuffle(table, table+10, randValue);
    }
    //-----------------------------------------------------------------------

    (Other than removing the first randValue() function, of course.)
     
    Juha Nieminen, Nov 30, 2011
    #1
    1. Advertising

  2. Juha Nieminen

    SG Guest

    On 30 Nov., 17:16, Leigh Johnston wrote:
    > On 30/11/2011 15:37, Juha Nieminen wrote:
    > >    Any suggestions how to make this compile?

    >
    > > //-----------------------------------------------------------------------
    > > #include<algorithm>
    > > #include<cstdlib>

    >
    > > // The actual rng replaced with std::rand() for simplicity
    > > unsigned randValue() { return std::rand(); }
    > > unsigned randValue(unsigned modulo) { return std::rand() % modulo; }

    >
    > > int main()
    > > {
    > >      int table[10];
    > >      std::random_shuffle(table, table+10, randValue);
    > > }
    > > //-----------------------------------------------------------------------

    >
    > >    (Other than removing the first randValue() function, of course.)

    >
    >         unsigned(*pf)(unsigned) = &randValue;
    >         std::random_shuffle(table, table+10, pf);


    Alternatives:

    (1) static_cast:

    random_shuffle(...,static_cast<unsigned(*)(unsigned)>(randValue));

    (2) A C++2011 lambda expression:

    random_shuffle(...,[](unsigned i){return randValue(i);});

    Cheers!
    SG
     
    SG, Dec 2, 2011
    #2
    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. Piotre Ugrumov
    Replies:
    3
    Views:
    375
    Nick Hounsome
    Jan 25, 2004
  2. Martin T.
    Replies:
    7
    Views:
    820
    Martin T.
    Mar 10, 2008
  3. Thomas
    Replies:
    2
    Views:
    1,100
    Thomas
    Dec 24, 2010
  4. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    255
    Sherm Pendley
    Oct 11, 2004
  5. Shriramana Sharma
    Replies:
    2
    Views:
    149
    Shriramana Sharma
    Jun 27, 2013
Loading...

Share This Page