Search code examples
boostmulti-indexboost-multi-index

Index with boost multi_index


How can I index a boost::multi_index container using a member function of class(that is being stored in the multi_index) that returns a constant reference of another class?

The error I get is :

error C2440: 'specialization' : cannot convert from 'overloaded-function' to 'RetClass (__thiscall StoreMe::* )(void) const'

Edit1:

This is a complete verifiable piece of similar code I created which has the same error,

#include "stdafx.h"
#include<multi_index_container.hpp>
#include<boost/multi_index/hashed_index.hpp>
#include<boost/multi_index/mem_fun.hpp>


class RetClass
{
    int a, b;

};

class StoreMe
 {
    RetClass ex;
public:
    void setId(RetClass a) {
        ex = a;
    };


    virtual const RetClass& getId() const { return ex; }

};

typedef boost::multi_index_container<
    StoreMe,
    boost::multi_index::indexed_by<
        boost::multi_index::hashed_non_unique<boost::multi_index::const_mem_fun<StoreMe,     RetClass, &StoreMe::getId> >
    >
> mi_storeMe;

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

TIA

-R


Solution

  • Use boost::multi_index::const_mem_fun.

    Edited after OP's additional information: the return type specified in const_mem_fun has to be exactly the same as that of the function you want to use for indexing. Note the differences in your current code:

    virtual const RetClass& getId() const;
    const_mem_fun<StoreMe, RetClass, &StoreMe::getId>
    

    So, change the const_mem_fun part as follows:

    const_mem_fun<StoreMe, const RetClass&, &StoreMe::getId>