boost::multi_index_container 与 random_acces 和 hashed_unique

boost::multi_index_container with random_acces and hashed_unique

我正在尝试创建具有唯一键的索引和键访问的容器:所以这是我的代码:

#include <QString>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/mem_fun.hpp>

class MyClass{
public:
    MyClass( const QString& name ): mName( name ){}
    QString name    ( ) const { return mName; }
    void    setName ( const QString& name ) { mName = name ;}
private:
    QString mName;
};


using namespace boost::multi_index;
typedef multi_index_container<
    MyClass,
    indexed_by<
        random_access<tag<struct RandomIndex>>,
        hashed_unique<
            tag<struct NameIndex>,
            const_mem_fun<MyClass, QString, &MyClass::name>
        >
    >
> Table;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Table table;
    for (MyClass v : { "first", "second", "third" }){
        table.push_back(v);
    }

    return a.exec();
}

当我尝试编译它时出现错误 boost_1_55_0\boost\functional\hash\extensions.hpp:269: 错误: C2664: 'size_t boost::hash_value(std::type_index)' : 无法将参数 1 从 'const QString' 转换为 'std::type_index' 没有可用的可以执行此转换的用户定义转换运算符,或者无法调用该运算符

如我所见,boost 对 Qt class QString 一无所知,但我找不到解决此问题的方法。有人可以解释如何修复我的代码吗?

您需要告诉 Boost 如何散列 QString

最简单的方法似乎是:

namespace boost {
    template <> struct hash<QString> {
        size_t operator()(QString const& v) const {
            return qHash(v);
        }
    };
}

完整的工作示例:

#include <QtCore/QString>
#include <QtCore/QHash>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/mem_fun.hpp>

class MyClass{
public:
    MyClass( const QString& name ): mName( name ){}
    QString name    ( ) const { return mName; }
    void    setName ( const QString& name ) { mName = name ;}
private:
    QString mName;
};

namespace boost {
    template <> struct hash<QString> {
        size_t operator()(QString const& v) const {
            return qHash(v);
        }
    };
}


using namespace boost::multi_index;
typedef multi_index_container<
    MyClass,
    indexed_by<
        random_access<tag<struct RandomIndex>>,
        hashed_unique<
            tag<struct NameIndex>,
            const_mem_fun<MyClass, QString, &MyClass::name>
        >
    >
> Table;

#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Table table;
    for (QString v : { "first", "second", "third" }){
        table.push_back(MyClass(v));
    }

    return a.exec();
}