0

Possible Duplicate:
Using local classes with STL algorithms

I'm trying to pass a locally defined function object to STL functions and it does not work. Where in the world is it declared that this ought not to be possible ? That looks rather appealing to me !

Here's code that fails:

int main() {
  std::vector<int> v;
  struct gen { int operator()(){return 4;} };
  std::cout<<gen()()<<std::endl; // works
  std::generate_n(std::back_inserter(v),4,gen()); // fails
}

Incidentally, the following does work, not because gen_s is a struct, but because gen_s::gen is a static function. This is not so cool because what's the use of a stateless generator ?

std:vector<int> v;
struct gen_s { static int gen() { return 4; };
std::generate_n(std::back_inserter(v),5,gen_s::gen);

So, what about locally defined function objects ?

Community
  • 1
  • 1
Gzorg
  • 809
  • 4
  • 10
  • 25

0 Answers0