|
Boost Users : |
From: Nelis Franken (nfranken_at_[hidden])
Date: 2006-08-21 11:58:04
Good day.
After an initial post on the comp.lang.c++ usergroup, I was pointed to using
boost::bind for binding member functions as predicates for STL functions
(original example applied to sort()). The technique to use Boost's bind()
works well, except when used with STL's random_shuffle(). The following code
demonstrates a working call (on sort()) and a call that generates an error
(on random_shuffle()), both using the same technique.
The code:
#include <algorithm>
#include <functional>
#include <vector>
#include <boost/bind.hpp>
using namespace std;
class Foo {
public:
Foo() {
for (int i=0; i < 5; i++) {
dataVector.push_back(i*10);
idVector.push_back(i);
}
}
bool isLess(int leftID, int rightID) {
return (dataVector[leftID] < dataVector[rightID]);
}
int customRand(int n) {
return (rand() % n);
}
void mySort() {
sort(idVector.begin(), idVector.end(), boost::bind(&Foo::isLess,
this, _1, _2));
}
void myShuffle() {
random_shuffle(dataVector.begin(), dataVector.end(),
boost::bind(&Foo::customRand, this, _1));
}
private:
vector<int> dataVector;
vector<int> idVector;
};
int main(int argc, char** argv) {
Foo myObj;
myObj.myShuffle();
myObj.mySort();
return 0;
}
The error:
sort.cpp: In member function `void Foo::myShuffle()':
sort.cpp:33: error: no matching function for call to
`random_shuffle(__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
std::vector<int, std::allocator<int> > >, boost::_bi::bind_t<int,
boost::_mfi::mf1<int, Foo, int>,
boost::_bi::list2<boost::_bi::value<Foo*>, boost::arg<1> > >)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:1766:
note: candidates are: void std::random_shuffle(_RandomAccessIterator,
_RandomAccessIterator, _RandomNumberGenerator&) [with
_RandomAccessIterator = __gnu_cxx::__normal_iterator<int*,
std::vector<int, std::allocator<int> > >, _RandomNumberGenerator =
boost::_bi::bind_t<int, boost::_mfi::mf1<int, Foo, int>,
boost::_bi::list2<boost::_bi::value<Foo*>, boost::arg<1> > >]
The code compiles and runs fine using VC7.1 (Visual Studio 2003), but
gives an error using g++ (GCC) 3.4.4 (cygming special). Any help will
be greatly appreciated.
Kind regards,
Nelis Franken
PS: A follow-up post on the exact same problem to comp.lang.c++ directed me
to post it on the Boost list, since it was deemed off-topic. I hope someone
else on this list has had a similar STL problem with random_shuffle() and
boost::bind?
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net