2012-02-04 96 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