Hi,
I have a boost multi index container thus.
using namespace boost::multi_index;
template < typename O >
class Container
{
public:
multi_index_container<
O,
indexed_by<
ordered_unique<
const_mem_fun< O, std::string, &O::name >
>
>
> _container;
};
As you can see, by this design every object I use to create this container has to have a member function returning a string with the name "name".
This is obviously not ideal. I tried a couple of ways of passing in the "key" but I can't get any of them to work..
I tried this..
using namespace boost::multi_index;
template < typename O, typename KT, typename KM >
class Container
{
public:
multi_index_container<
O,
indexed_by<
ordered_unique<
const_mem_fun< O, KT, &KM >
>
>
> _container;
};
int main( int c, char *v[] )
{
Container< Object, std::string, Object::name > container;
}
but no joy..
the compiler complains that Object::name isn't a type but I'm not sure how to correct this. And even if I work out how to supply a type to the template, I'll still need a concrete instance of "Object::name" to be used by the container..
maybe I have to hand in the types and then and in the member function at construction? but then how do I construct the container .. My head hurts!?!
Alexy, below, kindly offered this solution
using namespace boost::multi_index;
template < typename O, typename KT, KT (O::* KM)() >
class Container
{
public:
multi_index_container<
O,
indexed_by<
ordered_unique<
const_mem_fun< O, KT, KM >
>
>
> _container;
};
int main( int c, char *v[] )
{
Container< Object, std::string, &Object::name > container; // <<---- ERROR HERE
}
However, this produded the following compiler error.
Template parameter KM requires an expression of type std::string (Object::*)().
at the line marked..
Ok. It turns out this was my fault by handing in an incorrectly signatured "&Object::name" parameter... I have fixed this..