boost::function/boost::bind compilation error

Hi, I was trying to use boost function/bind to bind the first argument of a member function that takes two arguments, when I got into a compilation error that I can't quite decipher or understand, below is a sample representation of the problem, and then there's the compilation error, am I doing something incorrectly here? I have tried removing the boost::mem_fn instantiation with no difference. #include <boost/function.hpp> #include <boost/bind.hpp> #include <boost/mem_fn.hpp> #include <iostream> using namespace std; using namespace boost; class my_class { public: void test(bool b) { cout << "got " << b << endl; } private: }; int main() { my_class c; boost::function1<void, bool> test_fn; boost::bind(&my_class::test, &c, _2); /* causes a compilation error */ test_fn = boost::bind<void>(boost::mem_fn(&my_class::test), &c, _2); /* works fine */ test_fn = bind1st(mem_fun(&my_class::test), &c); test_fn(true); test_fn(false); return 0; } /usr/boost/include/boost/bind/arg.hpp: In constructor 'boost::arg<I>::arg(const T&) [with T = boost::arg<2> ()(), int I = 1]': /usr/boost/include/boost/bind.hpp:292: instantiated from 'void boost::_bi::list2<A1, A2>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = boost::_mfi::mf1<void, my_class, bool>, A = boost::_bi::list1<bool&>, A1 = boost::_bi::value<my_class*>, A2 = boost::arg<2>]' /usr/boost/include/boost/bind/bind_template.hpp:32: instantiated from 'typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&) [with A1 = bool, R = void, F = boost::_mfi::mf1<void, my_class, bool>, L = boost::_bi::list2<boost::_bi::value<my_class*>, boost::arg<2> >]' /usr/boost/include/boost/function/function_template.hpp:152: instantiated from 'static void boost::detail::function::void_function_obj_invoker1<FunctionObj, R, T0>::invoke(boost::detail::function::function_buffer&, T0) [with FunctionObj = boost::_bi::bind_t<void, boost::_mfi::mf1<void, my_class, bool>, boost::_bi::list2<boost::_bi::value<my_class*>, boost::arg<2> > >, R = void, T0 = bool]' /usr/boost/include/boost/function/function_template.hpp:904: instantiated from 'void boost::function1<R, T1>::assign_to(Functor) [with Functor = boost::_bi::bind_t<void, boost::_mfi::mf1<void, my_class, bool>, boost::_bi::list2<boost::_bi::value<my_class*>, boost::arg<2> > >, R = void, T0 = bool]' /usr/boost/include/boost/function/function_template.hpp:785: instantiated from 'typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, boost::function1<R, T1>&>::type boost::function1<R, T1>::operator=(Functor) [with Functor = boost::_bi::bind_t<void, boost::_mfi::mf1<void, my_class, bool>, boost::_bi::list2<boost::_bi::value<my_class*>, boost::arg<2> > >, R = void, T0 = bool]' test.cpp:28: instantiated from here /usr/boost/include/boost/bind/arg.hpp:37: error: creating array with negative size ('-0x00000000000000001') Regards, Ahmed

On Sun, Sep 6, 2009 at 1:23 AM, Ahmed Badran<ahmed.badran@gmail.com> wrote:
Hi,
I was trying to use boost function/bind to bind the first argument of a member function that takes two arguments, when I got into a compilation error that I can't quite decipher or understand, below is a sample representation of the problem, and then there's the compilation error, am I doing something incorrectly here?
I have tried removing the boost::mem_fn instantiation with no difference.
#include <boost/function.hpp> #include <boost/bind.hpp> #include <boost/mem_fn.hpp> #include <iostream>
using namespace std; using namespace boost;
class my_class { public: void test(bool b) { cout << "got " << b << endl; } private: };
int main() { my_class c; boost::function1<void, bool> test_fn; boost::bind(&my_class::test, &c, _2);
/* causes a compilation error */ test_fn = boost::bind<void>(boost::mem_fn(&my_class::test), &c, _2);
Don't need the mem_fn here - and the _2 should be _1 as well, as you've declared test_fn with 1 parameter, so use this: test_fn = boost::bind(&my_class::test, &c, _1);
/* works fine */ test_fn = bind1st(mem_fun(&my_class::test), &c);
test_fn(true); test_fn(false); return 0; }
<snip>
Regards, Ahmed
Stuart Dootson

AMDG Ahmed Badran wrote:
I was trying to use boost function/bind to bind the first argument of a member function that takes two arguments, when I got into a compilation error that I can't quite decipher or understand, below is a sample representation of the problem, and then there's the compilation error, am I doing something incorrectly here?
I have tried removing the boost::mem_fn instantiation with no difference.
#include <boost/function.hpp> #include <boost/bind.hpp> #include <boost/mem_fn.hpp> #include <iostream>
using namespace std; using namespace boost;
class my_class { public: void test(bool b) { cout << "got " << b << endl; } private: };
int main() { my_class c; boost::function1<void, bool> test_fn; boost::bind(&my_class::test, &c, _2);
/* causes a compilation error */ test_fn = boost::bind<void>(boost::mem_fn(&my_class::test), &c, _2);
Use _1 instead of _2. The placeholder number indicates the first argument to the function object created by bind. You're only passing a single bool argument in, so only _1 is a valid placeholder. In Christ, Steven Watanabe

Steven Watanabe wrote:
AMDG
Ahmed Badran wrote:
I was trying to use boost function/bind to bind the first argument of a member function that takes two arguments, when I got into a compilation error that I can't quite decipher or understand, below is a sample representation of the problem, and then there's the compilation error, am I doing something incorrectly here?
I have tried removing the boost::mem_fn instantiation with no difference.
#include <boost/function.hpp> #include <boost/bind.hpp> #include <boost/mem_fn.hpp> #include <iostream>
using namespace std; using namespace boost;
class my_class { public: void test(bool b) { cout << "got " << b << endl; } private: };
int main() { my_class c; boost::function1<void, bool> test_fn; boost::bind(&my_class::test, &c, _2);
/* causes a compilation error */ test_fn = boost::bind<void>(boost::mem_fn(&my_class::test), &c, _2);
Use _1 instead of _2. The placeholder number indicates the first argument to the function object created by bind. You're only passing a single bool argument in, so only _1 is a valid placeholder.
Thanks, for some reason I thought, the function originally takes two and since I'm binding the first, I want to let the second pass through, the documentation also has "The placeholder _N selects the argument at position N from the argument list passed at "call time."" But since I wasn't calling it then and there, I somehow seemed to skip it. So the _N is essentially the positional parameter of the output function from bind rather than the input function. Thanks again. -Ahmed
participants (3)
-
Ahmed Badran
-
Steven Watanabe
-
Stuart Dootson