Re: Local class problem
On Jun 3, 5:43 pm, eric <e...@bakkerspees.nl> wrote:
The following code does compile with Visual C++ express 2008 but not
with g++ version 4.1.3:
#include <vector>
#include <algorithm>
void foo(std::vector<int>& values)
{
struct filler {
filler() : val_(0) {}
int operator()() { return ++val_; }
int val_;
};
std::generate(values.begin(), values.end(), filler());
}
g++ gives the following error:
local-class.cpp: In function 'void foo(std::vector<int,
std::allocator<int> >&)':
local-class.cpp:12: error: no matching function for call to
'generate(__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
std::vector<int, std::allocator<int> > >, foo(std::vector<int,
std::allocator<int> >&)::filler)'
What's the problem? Visual C++ (and I as well) does certainly know what
function to call. Why doesn't g++? What can be done about it? Note that
struct filler is a throw-away class that I don't want to have in the
global namespace and that I don't even want to have as a nested class in
some other encompassing class of which foo() might be a method.
The current standard forbids passing local classes as template
parameters (it more complex than that as it involves linkage, but this
is the effect). It works in MSVC because it is an extension/preview
for a feature that will be available in the next release of the
standard.
In the mean time you can do:
#include <vector>
#include <algorithm>
#include <add-appropriate-header-name-for-tr1-bind-here>
void foo(std::vector<int>& values)
{
struct filler {
static int dummy(int& val) { return ++val; }
};
int val = 0;
std::generate(values.begin(), values.end(),
std::tr1::bind( &filler::dummy, std::tr1::ref(val) ) );
}
[note: not tested]
if your compiler already supports TR1 (recent gccs and MSVC do). Else
use Boost.Bind which is equivalent.
The above chunk of code works because in this case you are not passing
a local class type to bind but a plain function pointer.
HTH,
--
gpd
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]