std::transform and boost::bind won't compile

Hi, I'm trying to use boost::bind with a simple function to transform a vector, but I can't compile it. Any idea about what I could be doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double plus(double factor, double a1, double a2) {
    return a1 + factor * a2;
}          

int main() {
[...]
int n = 10;
std::vector<double> vector(n, 100);
std::vector<double> to_be_added(n, 1000);

std::transform(
            vector.begin(),
            vector.end(),
            to_be_added.begin(),
            vector.begin(),
            boost::bind(&plus, 10, _2, _3));
}


g++ -Wall -Iinclude -o test/test_bind test/test_bind.cpp
In file included from /usr/include/boost/bind.hpp:22:0,
                 from test/test_bind.cpp:6:
/usr/include/boost/bind/bind.hpp: In instantiation of ‘R boost::_bi::list3<A1, A2, A3>::operator()(boost::_bi::type<R>, F&, A&, long int) [with R = double; F = double (*)(double, double, double); A = boost::_bi::list2<double&, double&>; A1 = boost::_bi::value<int>; A2 = boost::arg<2>; A3 = boost::arg<3>]’:
/usr/include/boost/bind/bind_template.hpp:61:59:   required from ‘boost::_bi::bind_t<R, F, L>::result_type boost::_bi::bind_t<R, F, L>::operator()(A1&, A2&) [with A1 = double; A2 = double; R = double; F = double (*)(double, double, double); L = boost::_bi::list3<boost::_bi::value<int>, boost::arg<2>, boost::arg<3> >; boost::_bi::bind_t<R, F, L>::result_type = double]’
/usr/include/c++/4.7/bits/stl_algo.h:4990:2:   required from ‘_OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation) [with _IIter1 = __gnu_cxx::__normal_iterator<double*, std::vector<double> >; _IIter2 = __gnu_cxx::__normal_iterator<double*, std::vector<double> >; _OIter = __gnu_cxx::__normal_iterator<double*, std::vector<double> >; _BinaryOperation = boost::_bi::bind_t<double, double (*)(double, double, double), boost::_bi::list3<boost::_bi::value<int>, boost::arg<2>, boost::arg<3> > >]’
test/test_bind.cpp:24:43:   required from here
/usr/include/boost/bind/bind.hpp:382:98: error: ambiguous overload for ‘operator[]’ in ‘a[boost::_bi::storage3<A1, A2, boost::arg<I> >::a3_<boost::_bi::value<int>, boost::arg<2>, 3>]’
/usr/include/boost/bind/bind.hpp:382:98: note: candidates are:
/usr/include/boost/bind/bind.hpp:285:8: note: A1 boost::_bi::list2<A1, A2>::operator[](boost::arg<1>) const [with A1 = double&; A2 = double&]
/usr/include/boost/bind/bind.hpp:286:8: note: A2 boost::_bi::list2<A1, A2>::operator[](boost::arg<2>) const [with A1 = double&; A2 = double&]
/usr/include/boost/bind/bind.hpp:288:8: note: A1 boost::_bi::list2<A1, A2>::operator[](boost::arg<1> (*)()) const [with A1 = double&; A2 = double&] <near match>
/usr/include/boost/bind/bind.hpp:288:8: note:   no known conversion for argument 1 from ‘boost::arg<3>()’ to ‘boost::arg<1> (*)()’
/usr/include/boost/bind/bind.hpp:289:8: note: A2 boost::_bi::list2<A1, A2>::operator[](boost::arg<2> (*)()) const [with A1 = double&; A2 = double&] <near match>
/usr/include/boost/bind/bind.hpp:289:8: note:   no known conversion for argument 1 from ‘boost::arg<3>()’ to ‘boost::arg<2> (*)()’
make: *** [test_bind] Error 1
1
2
// boost::bind(&plus, 10, _2, _3));
boost::bind( &plus, 10, _1, _2 ) ) ;
I found a way to avoid that function altogether, with the composition of two binds:

1
2
3
4
5
boost::bind(
std::plus<double>(),
    boost::bind(
        std::multiplies<double>(), factor_, _2),
_1));


Both solutions work.
Last edited on
Topic archived. No new replies allowed.