Search code examples
functionbindmembertr1

for_each bind vector of vector resize


This is my first question. I gave up and will use a hand rolled functor for this, but I am curious as to how it is supposed to be done. The contrived example below is intended to resize all of the vectors in a vector to be of size 9, by filling them with nulls. The indicated line causes MinGW GCC 4.5.0 to spew a lot of template errors. I've tried several different permutations, but only posted the code that I consider to be "closest to correct" below. How should it be written? Note, I want to retain the two-argument version of resize.

#include <vector>
using std::vector;
#include <algorithm>
using std::for_each;
#include <tr1/functional>
using std::tr1::bind;
using std::tr1::placeholders::_1;

int main() {
 vector<vector<void *> > stacked_vector(20);
 for_each(stacked_vector.begin(),stacked_vector.end(),
  bind(&std::vector<void *>::resize,_1,9,0/*NULL*/));  // voluminous error output
 return 0;
}

Thank you very much for your input.


Solution

  • It's hard to say without seeing the error output (and frankly, even with it). However, try passing the NULL as a void* type: static_cast<void*>(0). Otherwise the object returned by bind tries to give an int value as the second parameter to resize.