gpt4 book ai didi

c++ - 使用 std::reference_wrapper 作为 std::map 中的键

转载 作者:可可西里 更新时间:2023-11-01 16:33:18 30 4
gpt4 key购买 nike

我在类层次结构中有一堆对象,我想制作一个 std::map使用对这些对象的引用作为映射中的键。它看起来像 std::reference_wrapper正是为此所需要的,但我似乎无法让它发挥作用。到目前为止我尝试了什么:

class Object { // base class of my hierarchy
// most details unimportant
public
virtual bool operator< (const Object &) const; // comparison operator
};

std::map<std::reference_wrapper<const Object>, int> table;

auto it = table.find(object);

table[object] = 42;

table[object]++

但是,我总是从编译器中得到一些模糊的错误:

/usr/include/c++/4.5.3/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::reference_wrapper<const Object>]’:
/usr/include/c++/4.5.3/bits/stl_tree.h:1522:38: instantiated from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) [with _Key = std::reference_wrapper<const Object>, _Val = std::pair<const std::reference_wrapper<const Object>, int>, _KeyOfValue = std::_Select1st<std::pair<const std::reference_wrapper<const Object>, int> >, _Compare = std::less<std::reference_wrapper<const Object> >, _Alloc = std::allocator<std::pair<const std::reference_wrapper<const Object>, int> >, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::reference_wrapper<const Object>, int> >]’
/usr/include/c++/4.5.3/bits/stl_map.h:697:29: instantiated from ‘std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::find(const key_type&)[with _Key = std::reference_wrapper<const Object>, _Tp = int, _Compare = std::less<std::reference_wrapper<const Object> >, _Alloc = std::allocator<std::pair<const std::reference_wrapper<const Object>, int> >, std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::reference_wrapper<const Object>, int> >, key_type = std::reference_wrapper<const Object>]’
testfile.cpp:39:31: instantiated from here
/include/c++/4.5.3/bits/stl_function.h:230:22: error: no match for ‘operator<’ in ‘__x < __y’

错误似乎是说它无法比较两个 std::reference_wrapper<const Object>对象,但它似乎应该是可能的 -- std::reference_wrapper有一个转换运算符,可以将其隐式转换为 T& (此处为 const Object &)和 Object有一个 operator < ,那么为什么它不起作用?

它应该工作吗,这只是 g++ 中的一个错误?还是发生了其他事情?

最佳答案

默认 std::map 使用 std::less<std::reference_wrapper<const Object>>作为Compare ,但是std::reference_wrapper<T>不转发operator<()到基础类型 T .

解决问题的最简单和最简洁的选择是定义 std::less<const Object> (或 std::greater<const Object> )在 map 定义中是这样的:

std::map<std::reference_wrapper<const Object>, int, std::less<const Object>> table;

由于 implicit conversion of std::reference_wrapper to T& ,它将按预期正常工作和 implicit constructor of std::reference_wrapper .

Example .

关于c++ - 使用 std::reference_wrapper 作为 std::map 中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9139748/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com