使用std :: reference

编程入门 行业动态 更新时间:2024-10-26 19:27:01
本文介绍了使用std :: reference_wrapper作为std :: map中的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个类层次结构中的一堆对象,并希望使用对这些对象的引用作为地图中的键的 std :: map 。它看起来像 std :: reference_wrapper 将是正是这需要的,但我似乎无法使它的工作。到目前为止我尝试了:

class Object {//我的层次结构的基类 //最详细的unimportant public virtual bool operator< (const Object&)const; //比较运算符}; std :: map< std :: reference_wrapper< const Object>,int>表; auto it = table.find(object); table [object] = 42; table [object] ++

但是,我总是有些模糊来自编译器的错误:

/ usr / include / c ++ / 4.5.3 / bits / stl_function.h: 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:从'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& 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> >]' /usr/include/c++/4.5.3/bits/stl_map.h:697:29:从'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& ; >,_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> >,key_type = std :: reference_wrapper< const Object>]' testfile.cpp:39:31:从这里实例化 /include/c++/4.5.3/bits/stl_function.h:230 :22:error:没有匹配'operator'<'in'__x< __y'

错误似乎是说它不能比较两个 std :: reference_wrapper< const Object> 对象,但它似乎应该是可能的 - std :: reference_wrapper 有一个转换操作符可以隐式将它转换为 T& ( const Object& code>有运算符< ,为什么它不工作?

这只是g ++中的一个错误?

解决方案

似乎如果你让比较运算符是一个自由函数调用虚拟成员函数)。

如果它是一个成员函数, a< b 真的表示 a.operator<(b); ,并且左侧参数不考虑隐式转换。 $ b

I have a bunch of objects in a class hierarchy and would like to make a std::map using references to those objects as the keys in the map. Its seems like std::reference_wrapper would be exactly what is needed for this, but I can't seem to make it work. What I've tried so far:

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]++

However, I always get somewhat obscure errors from the compiler:

/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’

The error seems to be saying it can't compare two std::reference_wrapper<const Object> objects, but it seems like it should be possible -- std::reference_wrapper has a conversion operator that can implicitly convert it to a T& (const Object & here), and Object has a operator <, so why doesn't it work?

Should it work and this is merely a bug in g++? Or is something else going on?

解决方案

It seems that it would work if you made the comparison operator a free function (that perhaps calls a virtual member function).

If it is a member function, a < b really means a.operator<(b); and implicit conversions are not considered for the left-side argument.

更多推荐

使用std :: reference

本文发布于:2023-08-04 17:28:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:std   reference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!