Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boost多索引插入错误,其中boost::tuple引用作为有序唯一索引的键_C++_Boost Multi Index_Boost Tuples - Fatal编程技术网

C++ boost多索引插入错误,其中boost::tuple引用作为有序唯一索引的键

C++ boost多索引插入错误,其中boost::tuple引用作为有序唯一索引的键,c++,boost-multi-index,boost-tuples,C++,Boost Multi Index,Boost Tuples,我已经把它归结为我能想到的最简单的示例代码 我有一个由成员编制的多索引boost: typedef const boost::tuple<const uint32_t &, const uint8_t &> key_type; typedef const boost::元组键类型; 这样做似乎会使多索引认为每个项目都是相等的(大小从不>1) 我正在存储一个包含2个成员的结构,我希望多索引的唯一键是这两个成员。我认为制作一组引用将非常简单地完成这一任务。不过,它的表

我已经把它归结为我能想到的最简单的示例代码

我有一个由成员编制的多索引boost:

typedef const boost::tuple<const uint32_t &, const uint8_t &> key_type;
typedef const boost::元组键类型;
这样做似乎会使多索引认为每个项目都是相等的(大小从不>1)

我正在存储一个包含2个成员的结构,我希望多索引的唯一键是这两个成员。我认为制作一组引用将非常简单地完成这一任务。不过,它的表现并不像我预期的那样。当元组中的项是引用时,似乎每个新项都与现有项冲突。可能还值得注意的是,简单地远离引用将使代码按我预期的方式运行,但这并不能帮助我理解为什么引用案例不起作用

#include <stdint.h>
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/tag.hpp>
#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"

namespace bmi = ::boost::multi_index;

class MyMultiIndex {
public:
    MyMultiIndex() {}
    ~MyMultiIndex() {}

    // Switching away from references fixes everything....
    typedef const boost::tuple<const uint32_t &, const uint8_t &> key_type;
    //typedef const boost::tuple<const uint32_t, const uint8_t> key_type;

    struct Item {
        const uint8_t thing1;
        const uint32_t thing2;
        key_type key;

        Item(const uint8_t &a1, const uint32_t &a2)
                : thing1(a1), thing2(a2), key(thing2, thing1)
        {}
    };

    struct key_idx {};

    typedef bmi::multi_index_container<
        Item,
        bmi::indexed_by<
            bmi::ordered_unique<bmi::tag<key_idx>,
                                bmi::member<Item, key_type, &Item::key>
            >
        >
    > imsi_map_type;

    typedef imsi_map_type::index<key_idx>::type key_idx_type;

    void insert(const uint8_t &a1, const uint32_t &a2)
    {
        Item item(a1, a2);

        key_idx_type &idx(mi.get<key_idx>());
        std::pair<key_idx_type::iterator, bool> ret = idx.insert(item);

        if (!ret.second) {
            std::cout << "itr = " << (int)ret.first->thing1 << " " << ret.first->thing2 << std::endl;
        }
    }
private:
    imsi_map_type mi;
};

int
main()
{
    MyMultiIndex mindex;

    mindex.insert(1, 10);
    mindex.insert(1, 20);
    mindex.insert(3, 10);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“boost/tuple/tuple.hpp”
#包括“boost/tuple/tuple\u comparison.hpp”
命名空间bmi=::boost::multi_索引;
类MyMultiIndex{
公众:
MyMultiIndex(){}
~MyMultiIndex(){}
//从引用中切换可以修复一切。。。。
typedef const boost::元组键类型;
//typedef const boost::元组键类型;
结构项{
建筑材料1;
建筑材料2;
按键式按键;
项目(施工单位8、a1、32、a2)
:第1件(a1),第2件(a2),钥匙(第2件,第1件)
{}
};
结构键_idx{};
typedef bmi::多索引容器<
项目,,
bmi::按索引索引<
bmi::有序_唯一
>
>imsi_映射_类型;
typedef imsi_map_type::index::type key_idx_type;
无效插入物(常数uint8\U t&a1、常数uint32\U t&a2)
{
项目(a1、a2);
key_idx_type&idx(mi.get());
std::pair ret=idx.insert(项目);
如果(!ret.second){

std::coutItem
的复制语义(由其默认复制构造函数实现)有缺陷。请提供如下复制构造函数:

Item(const Item& x)
  : thing1(x.thing1), thing2(x.thing2), key(thing2, thing1)
{}

没有人关心你的观点。打开此页面的人不一定要拼凑出你的意思。只需写一篇完整、清晰的正文。这是一个好观点。我已经改变了当前的问题(希望更好)在发布将来的问题时,请记住这一点。至于您想解决的初始问题,Boost.MultiIndex提供了复合键,而无需使用您正在试验的元组结构:对于我来说,多索引作者在S.O.上的贡献是多么幸运:)