boost::multiindex 和继承
boost::multiindex and inheritance
我正在尝试从 boot::multiindex 继承并看看我能做些什么,同时插入工作正常但替换不是。
代码
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <string>
#include <utility>
#include <iostream>
using namespace boost::multi_index;
struct employee
{
std::string name_;
int id;
int state;
employee(std::string name, int id)
: name_{std::move(name)},
id(id),
state(0) {}
employee(const employee& copy):
name_(copy.name_),
id(copy.id),
state(copy.state){}
bool operator<(const employee &a) const
{
return id < a.id;
}
const std::string& get_name() const
{
return name_;
}
};
struct names{};
typedef multi_index_container<
employee,
indexed_by<
ordered_unique<
identity<employee>
>,
hashed_unique<
tag<names>,
const_mem_fun<
employee, const std::string&, &employee::get_name
>
>
>
> employee_container;
typedef employee_container::index<names>::type::iterator employee_iterator_by_name;
//using employee_by_name = employee_container::nth_index<ANTENNA_INDEX_BY_NAME>::type&;
class employee_db: public employee_container
{
public:
void add_db(const employee& e)
{
this->insert(e);
}
void update_db(
employee_iterator_by_name& it
)
{
this->replace(it, *it);
}
};
在Linux
中编译它
gcc version 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)
[sandbox@localhost multiindex]$ rpm -qa | grep boost | grep devel
boost-devel-1.66.0-7.el8.x86_64
[sandbox@localhost multiindex]$ g++ 2.cc -c -std=c++11
2.cc: In member function ‘void employee_db::update_db(employee_iterator_by_name&)’:
2.cc:75:31: error: no matching function for call to ‘employee_db::replace(employee_iterator_by_name&, const value_type&)’
this->replace(it, *it);
我做错了什么?
在 update_db
中,您在 replace
的索引 #0 版本(默认索引)上使用 employee_iterator_by_name
,它是索引 #1 的迭代器。您要使用索引 #1 replace
:
void update_db(employee_iterator_by_name& it)
{
this->get<names>().replace(it, *it);
}
我知道这只是一些探索性代码,因为以这种方式调用 replace
不会改变任何东西(您正在用指向的值替换 it
指向的值通过 it
).
我正在尝试从 boot::multiindex 继承并看看我能做些什么,同时插入工作正常但替换不是。
代码
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <string>
#include <utility>
#include <iostream>
using namespace boost::multi_index;
struct employee
{
std::string name_;
int id;
int state;
employee(std::string name, int id)
: name_{std::move(name)},
id(id),
state(0) {}
employee(const employee& copy):
name_(copy.name_),
id(copy.id),
state(copy.state){}
bool operator<(const employee &a) const
{
return id < a.id;
}
const std::string& get_name() const
{
return name_;
}
};
struct names{};
typedef multi_index_container<
employee,
indexed_by<
ordered_unique<
identity<employee>
>,
hashed_unique<
tag<names>,
const_mem_fun<
employee, const std::string&, &employee::get_name
>
>
>
> employee_container;
typedef employee_container::index<names>::type::iterator employee_iterator_by_name;
//using employee_by_name = employee_container::nth_index<ANTENNA_INDEX_BY_NAME>::type&;
class employee_db: public employee_container
{
public:
void add_db(const employee& e)
{
this->insert(e);
}
void update_db(
employee_iterator_by_name& it
)
{
this->replace(it, *it);
}
};
在Linux
中编译它gcc version 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)
[sandbox@localhost multiindex]$ rpm -qa | grep boost | grep devel
boost-devel-1.66.0-7.el8.x86_64
[sandbox@localhost multiindex]$ g++ 2.cc -c -std=c++11
2.cc: In member function ‘void employee_db::update_db(employee_iterator_by_name&)’:
2.cc:75:31: error: no matching function for call to ‘employee_db::replace(employee_iterator_by_name&, const value_type&)’
this->replace(it, *it);
我做错了什么?
在 update_db
中,您在 replace
的索引 #0 版本(默认索引)上使用 employee_iterator_by_name
,它是索引 #1 的迭代器。您要使用索引 #1 replace
:
void update_db(employee_iterator_by_name& it)
{
this->get<names>().replace(it, *it);
}
我知道这只是一些探索性代码,因为以这种方式调用 replace
不会改变任何东西(您正在用指向的值替换 it
指向的值通过 it
).