std::bind1st making life difficult

Discussion in 'C++' started by pillbug, May 6, 2006.

  1. pillbug

    pillbug Guest

    I'm having trouble using bind1st with mem_fun1. I can't compile
    the line marked below with a @@. If I use the join2 method everything
    is fine, but I wind up passing string vectors by value. I would like
    to use the reference passing version, or something similar.

    The errors I get point the finger at binder1st::eek:perator() being defined
    as "const argument_type&" which becomes "const const string_vector&&"
    when i use Joiner::join below.

    Any advice would be appreciated. I use vc7.1

    #include <string>
    #include <vector>
    #include <functional>
    #include <algorithm>
    #include <numeric>
    #include <iostream>

    typedef std::basic_string<char> string;
    typedef std::vector<string> string_vector;

    struct Joiner {
    string join (const string_vector& v) {
    return std::accumulate (v.begin(), v.end(), string()); }
    string join2 (string_vector v) {
    return std::accumulate (v.begin(), v.end(), string()); }
    };

    int main (int argc, char** argv)
    {
    // a 10x7 matrix of strings initialized to "Text"
    std::vector<string_vector> vv (10, string_vector (7, "Text"));

    Joiner j;
    string_vector result (vv.size());
    std::transform (vv.begin(), vv.end(),
    result.begin(),
    //std::bind1st (std::mem_fun1 (&Joiner::join), &j)); //error
    std::bind1st (std::mem_fun1 (&Joiner::join2), &j));

    std::copy (result.begin(), result.end(),
    std::eek:stream_iterator<string> (std::cout, "\n"));

    return 1;
    }
     
    pillbug, May 6, 2006
    #1
    1. Advertising

  2. * pillbug:
    > I'm having trouble using bind1st with mem_fun1. I can't compile
    > the line marked below with a @@. If I use the join2 method everything
    > is fine, but I wind up passing string vectors by value. I would like
    > to use the reference passing version, or something similar.
    >
    > The errors I get point the finger at binder1st::eek:perator() being defined
    > as "const argument_type&" which becomes "const const string_vector&&"
    > when i use Joiner::join below.
    >
    > Any advice would be appreciated. I use vc7.1
    >
    > #include <string>
    > #include <vector>
    > #include <functional>
    > #include <algorithm>
    > #include <numeric>
    > #include <iostream>


    Pedantic: #include <ostream> here (not that your compiler will complain).


    > typedef std::basic_string<char> string;


    Please don't define a name that's used by the standard library; it only
    sows confusion, and acts as a bug-attractor.


    > typedef std::vector<string> string_vector;


    Good.


    > struct Joiner {
    > string join (const string_vector& v) {
    > return std::accumulate (v.begin(), v.end(), string()); }
    > string join2 (string_vector v) {
    > return std::accumulate (v.begin(), v.end(), string()); }
    > };


    Is there perhaps some reason why these functions are non-const member
    functions? I'll assume there is. But in the context of the code you've
    shown, there's none: they should at least be const, preferably static,
    or perhaps non-member.


    > int main (int argc, char** argv)
    > {
    > // a 10x7 matrix of strings initialized to "Text"
    > std::vector<string_vector> vv (10, string_vector (7, "Text"));
    >
    > Joiner j;
    > string_vector result (vv.size());
    > std::transform (vv.begin(), vv.end(),
    > result.begin(),
    > //std::bind1st (std::mem_fun1 (&Joiner::join), &j)); //error
    > std::bind1st (std::mem_fun1 (&Joiner::join2), &j));
    >
    > std::copy (result.begin(), result.end(),
    > std::eek:stream_iterator<string> (std::cout, "\n"));
    >
    > return 1;


    That's an undefined return value, but in practice it maps to "error".
    You'd want to indicate "success". Do that by omitting return, or by
    returning 0 (which is the default for main), or EXIT_SUCCESS.

    > }



    Regarding the error, try

    class JoinerFunc: public std::unary_function<string_vector, string>
    {
    private:
    Joiner* myJ;
    public:
    JoinerFunc( Joiner* j ): myJ( j ) {}
    string operator()( string_vector const& v ) const
    { return myJ->join( v ); }
    };

    int main()
    {
    // a 10x7 matrix of strings initialized to "Text"
    std::vector<string_vector> vv (10, string_vector (7, "Text"));

    Joiner j;
    string_vector result (vv.size());
    std::transform(
    vv.begin(), vv.end(), result.begin(), JoinerFunc( &j )
    );

    std::copy (result.begin(), result.end(),
    std::eek:stream_iterator<string> (std::cout, "\n"));
    }


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 6, 2006
    #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. wenmang
    Replies:
    1
    Views:
    513
    Victor Bazarov
    Aug 8, 2003
  2. Fred Ma
    Replies:
    2
    Views:
    2,091
    Fred Ma
    Feb 5, 2004
  3. Denis Remezov

    binary_function and bind1st

    Denis Remezov, May 19, 2004, in forum: C++
    Replies:
    6
    Views:
    770
    P.J. Plauger
    May 21, 2004
  4. Marc
    Replies:
    6
    Views:
    698
    Denis Remezov
    Jul 6, 2004
  5. Nick Keighley
    Replies:
    5
    Views:
    306
    Andre Kostur
    Aug 4, 2005
Loading...

Share This Page