I’m using boost::multi_index_container and am trying to get a modify operation working. My modification routine looks like this (roughly), using a function inner class:
void FooContainer::modifyAttribute(string key, int newValue) {
struct ModifyFunc {
int val;
ModifyFunc(int val): val(val) {}
void operator()(Foo &f) {
foo.val = val;
}
};
StorageContainer::index<keyTag>::type &idx = mContainer.get<keyTag>();
StorageContainer::index<keyTag>::type::iterator iter = idx.find(key);
idx.modify(iter, ModifyFunc(newValue));
}
When I try to compile this, I get a multi-page spew of compiler error like this (most of it omitted):
FooContainer.cpp:##: error: no matching function for call to [...]::modify([...]&, FooContainer::modifyAttribute(string,int)::ModifyFunc)’
What’s wrong with this invocation and how can I make it work?
The problem is that function inner classes aren’t recognized by the compiler as a valid typename for template parameters; it isn’t obvious, but the
multi_index_container::index<T>::type::modifymethod uses the type of the modify parameter as a template argument which is a detail normally hidden from view. However, look at its declaration in, for example,boost/multi_index/hashed_index.hpp:The easy fix is to make the
ModifyFuncstruct not a function inner class; trivially, make it inner toFooContainerrather than to theFooContainer::modifyAttributemethod. Of course, this also means that you can reuse theModifyFuncclass elsewhere, if it turns out you need it in more than one place.