tags:

views:

67

answers:

1

Hi!

I cannot compile following code by g++ 4.3.2:

#include <stdlib.h>
#include <algorithm>

struct Generator {
  ptrdiff_t operator() (ptrdiff_t max) {
    return rand() % max;
  }
};

// ...
Generator generator;
std::vector<size_t> indices;
// fill vector
std::random_shuffle(indices.begin(), indices.end(), generator); // error here!

Why my compiler throws following error in last line?

error: no matching function for call to ‘random_shuffle(__gnu_cxx::__normal_iterator<long unsigned int*, std::vector<long unsigned int, std::allocator<long unsigned int> > >, __gnu_cxx::__normal_iterator<long unsigned int*, std::vector<long unsigned int, std::allocator<long unsigned int> > >, Generator&)’

Thanks in advance!

A: 

Actually in my code Generator structure is declared within a function. I don't know wjy, but g++ 4.3.2 does not accept this. After I moved structure declaration out of function, compilation successes. I believe this is a bug in compiler. Moreover original code was successfully compiled by earlier version of g++.

Andrey
You cannot instantiate a template with a local type. `random_shuffle` is a class template, and `Generator` is a local type. This is why it works if you move `Generator` so that it is not a local type.
James McNellis