2012-02-04 93 views
6

我在类层次结构中有一堆对象,并且想要使用对这些对象的引用作为映射中的键的std::map。它好像std::reference_wrapper就是这个所需要的,但我似乎无法使它工作。我试过到目前为止:使用std :: reference_wrapper作为std :: map中的键

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 ++中的一个错误?或者还在发生其他事情?

+0

'std :: ref(object) dalle 2012-02-04 09:58:47

回答

5

看起来,如果您将比较运算符设为一个自由函数(可能调用虚拟成员函数),它就会起作用。

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

1

在Visual Studio 11 Beta上,我遇到了同样的问题。 使用调用<运算符的免费版本解决了该问题。

#include<map> 
#include<iostream> 
using namespace::std; 

class Object { 
    int _n1; 
public: 

    Object(int n = 0):_n1(n){}; 
    bool operator < (const Object& rhs) const {return this->_n1 < rhs._n1;} 
    friend ostream &operator << (ostream &stream, const Object& o) { stream << o._n1 << " "; return stream;} 
}; 

struct ObjectLess{ 

    bool operator()(const Object& lhs, const Object& rhs) const 
    { 
     return lhs<rhs; 
    } 
}; 

int main(int argc, char* argv[]) 
{ 
    //This does not compile 
    //std::map<std::reference_wrapper<const Object>, string> table; 

    //Using the free function works 
    std::map<std::reference_wrapper<const Object>, string, ObjectLess> table; 

    Object a(1); 
    Object b(2); 
    Object c(3); 


    table[a]="One"; 
    table[c]="Three"; 
    table[b]="Two"; 

    for(auto y: table){ 
    cout << y.first << " " << y.second.c_str() << std::endl; 
} 


    return 0; 
} 
3

默认std::less<std::reference_wrapper<const Object>>被使用,但它前进operator<()不为基础类型。

为您解决问题的最简单和concisest选项是在这样的地图定义来定义std::less<const Object>(或std::greater<const Object>):

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