std::bind1st making life difficult

P

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>

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;
}
 
A

Alf P. Steinbach

* 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>

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"));
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top