不能使用 const 参数来查找 boost::multi_index_container 的 () 方法
can't use a const argument to find() method of the boost::multi_index_container
我有一个非常简单的多索引容器,它索引类的成员如下:
基类:
class AgentInfo {
public:
Agent * agent;
bool valid;
AgentInfo(Agent * a_, bool valid = true);
AgentInfo();
};
//helper tags used in multi_index_container
struct agent_tag{};
struct agent_valid{};
声明multi_index_container的类:
class AgentsList {
public:
private:
//main definition of the container
typedef boost::multi_index_container<
AgentInfo, indexed_by<
random_access<>//0
,hashed_unique<tag<agent_tag>,member<AgentInfo, Agent *, &AgentInfo::agent> >//1
,hashed_non_unique<tag<agent_valid>,member<AgentInfo, bool, &AgentInfo::valid> >//2
>
> ContainerType;
//the main storage
ContainerType data;
//easy reading only
typedef typename nth_index<ContainerType, 1>::type Agents;
public:
// given an agent's reference, returns a const version of a single element from the container.
const AgentInfo &getAgentInfo(const Agent * agent, bool &success)const {
success = false;
AgentsList::Agents &agents = data.get<agent_tag>();
AgentsList::Agents::iterator it;
if((it = agents.find(agent)) != agents.end())//<-- error in .find()
{
success = true;
}
return *it;
}
};
我的问题在于getAgentInfo()
这是一种访问器方法。错误很明显:
error: invalid conversion from ‘const Agent*’ to ‘Agent*’
我无法输入非常量
Agent*
因为我是从代码库的其他部分获取的。我不喜欢使用
const_cast
有没有办法使用常量代理调用.find()
方法?谢谢
将索引 1 定义为
hashed_unique<
tag<agent_tag>,
member<AgentInfo, Agent *, &AgentInfo::agent>,
boost::hash<const Agent*>, std::equal_to<const Agent*>
>
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- DrawIndexedInstanced 具有不同的 Index Count per Instance (Directx
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- OpenGL VBO Indexing ( How to compute Index Array)
- "Warning: Comma within array index expression"但逗号分隔函数参数
- std::p mr::memory_resource 如何与 std::container 产生性能差异?
- 如何根据排序索引的向量对 std::index 集进行排序?
- 在一行代码中理解 index++
- C++ "oldstyle container ":指针/数组/新 - 可能误会?
- 在其他容器中使用 boost::container::static_vector 时,GCC 编译错误"将'const s'绑定到类型's&'的引用丢弃限定符"
- 为什么打印 c 样式字符串的'address of index n'会导致子字符串的输出
- 构造函数是否有一种现代C++方法来了解其'container'类?
- 使用 std::get 访问 std::variant<index>
- 代码中的"vector<<int>vector>&index"是什么意思?
- 为什么std::{container}::template不能推导其参数类型
- CStringArray::GetAt(int index) 返回一个 const. 为什么?
- 如何配置 Doxygen 以在"Class Index"中包含类的类型定义?
- boost::container::vector 无法用谷物序列化?
- 如何从 boost::container::vector<std::string>::iterator 访问索引和对象?
- 模板化函数以从输入参数推断返回类型 stl-container