std::bind1st making life difficult



 DEVELOP > c-Plus-Plus > std::bind1st making life difficult

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "pillbug"
Date: 06 May 2006 03:44:55 AM
Object: std::bind1st making life difficult
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::operator() 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::ostream_iterator<string> (std::cout, "\n"));
return 1;
}
.

User: "Alf P. Steinbach"

Title: Re: std::bind1st making life difficult 06 May 2006 05:08:58 AM
* 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::operator() 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::ostream_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::ostream_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?
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER