I'm trying to use bind with boost.thread in order to make a thread of a class
member function with an input argument, but I can't get it to compile. To narrow
the problem, I went to the bounded_buffer.cpp program in the
libs/thread/tutorial dir. I added an int arg to sender, and then changed
boost::thread thrd1(&sender);
to
int x = 99;
boost::thread thrd1(boost::bind<void>(&sender, _1)(x));
and several other combinations including removal of <void>. In the above case,
gcc results in
/mnt/shared/Projects/boosttest/src/bounded_buffer.cpp:77: error: call of
overloaded `bind(void (*)(int), boost::arg<1>&)' is ambiguous
/usr/include/boost/bind.hpp:1132: note: candidates are: boost::_bi::bind_t
Jason wrote:
I'm trying to use bind with boost.thread in order to make a thread of a class member function with an input argument, but I can't get it to compile. To narrow the problem, I went to the bounded_buffer.cpp program in the libs/thread/tutorial dir. I added an int arg to sender, and then changed boost::thread thrd1(&sender); to int x = 99; boost::thread thrd1(boost::bind<void>(&sender, _1)(x));
You need boost::bind(sender, x), assuming that sender is a void function taking an int.
I got this syntax from the bind doc (http://www.boost.org/libs/bind/bind.html). What am I doing wrong?
Hmm... the examples in the documentation bind and call on a single line in order to demonstrate the effect of the various possibilities. It can be confusing. :-) Your case is covered by the first sentence: "bind(f, 1, 2) will produce a "nullary" function object that takes no arguments and returns f(1, 2)."
Jason wrote:
int x = 99; boost::thread thrd1(boost::bind<void>(&sender, _1)(x));
and several other combinations including removal of <void>. In the above case, gcc results in /mnt/shared/Projects/boosttest/src/bounded_buffer.cpp:77: error: call of overloaded `bind(void (*)(int), boost::arg<1>&)' is ambiguous /usr/include/boost/bind.hpp:1132: note: candidates are: boost::_bi::bind_t
boost::bind(F, A1) [with R = void, F = void (*)(int), A1 = boost::arg<1>]
I am having a very similar (I think) problem.
Namely, I'm playing with the Spirit library. I started with
the comma-separated-real-number-parser from the examples
but wanted to use my own semantic action:
void F (vector<double> &v, double x) {
v.push_back(x);
}
the core function is:
bool parse_numbers(char const* str, vector<double> &v) {
return parse(str,
real_p[bind<void>(&F,v,_1)] >>
*(',' >> real_p[bind<void>(&F,v,_1)]),
space_p).full;
}
on both lines with "bind" I get:
test.cpp: In function `<snip>':
test.cpp:19: error: call of overloaded `
Misza wrote:
I am having a very similar (I think) problem.
Namely, I'm playing with the Spirit library. I started with the comma-separated-real-number-parser from the examples but wanted to use my own semantic action:
void F (vector<double> &v, double x) { v.push_back(x); }
the core function is:
bool parse_numbers(char const* str, vector<double> &v) { return parse(str, real_p[bind<void>(&F,v,_1)] >> *(',' >> real_p[bind<void>(&F,v,_1)]), space_p).full; }
I've no idea what is the exact problem you're having, but one thing's for sure: bind(&F, v, _1) stores a copy of v. I don't think that this is what you want. Use ref(v). You don't need to use <void> with ordinary functions, and the & in front of F isn't needed, too. bind(&vector<double>::push_back, &v, _1) is also possible.
"Peter Dimov"
bool parse_numbers(char const* str, vector<double> &v) { return parse(str, real_p[bind<void>(&F,v,_1)] >> *(',' >> real_p[bind<void>(&F,v,_1)]), space_p).full; }
I've no idea what is the exact problem you're having, but one thing's for sure: bind(&F, v, _1) stores a copy of v. I don't think that this is what you want. Use ref(v). You don't need to use <void> with ordinary functions, and the & in front of F isn't needed, too. bind(&vector<double>::push_back, &v, _1) is also possible.
Technically it's undefined behavior to take the address of a standard library [member] function, but you could probably get away with it. -- Dave Abrahams Boost Consulting www.boost-consulting.com
I have managed to go around the "overload" problem by switching to the Lambda library. I still had a problem with "couldn't initialize `const vector<double> &' with `vector<double> &'", but using ref(v) solves that as well. Thanks, Misza
participants (4)
-
David Abrahams
-
Jason
-
Misza
-
Peter Dimov