2012-02-04 16 views
6

Mam kilka obiektów w hierarchii klas i chciałbym, aby std::map odwoływał się do tych obiektów jako kluczy na mapie. Wygląda na to, że std::reference_wrapper jest dokładnie tym, czego potrzebujemy, ale nie mogę sprawić, żeby działało. Co próbowałem dotąd:Używanie std :: reference_wrapper jako klucza w 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]++ 

Jednak zawsze pojawiają się błędy nieco niejasne z kompilatora:

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

Błąd zdaje się mówić, że nie można porównywać dwa std::reference_wrapper<const Object> obiektów, ale wydaje się, że powinno być możliwe - std::reference_wrapper ma operator konwersji, który może domyślnie przekonwertować go na T& (const Object & tutaj), a Object ma operator <, więc dlaczego to nie działa?

Czy to działa i jest to tylko błąd w g ++? Czy coś jeszcze się dzieje?

+0

Czy 'std :: ref (obiekt) dalle

Odpowiedz

5

Wygląda na to, że działałoby, gdyby operator porównania był darmową funkcją (która może wywoływać funkcję wirtualnego członka).

Jeśli jest to funkcja członka, a < b naprawdę oznacza a.operator<(b);, a niejawne konwersje nie są brane pod uwagę dla argumentu po lewej stronie.

1

W programie Visual Studio 11 Beta pojawia się ten sam problem. Korzystanie z bezpłatnej wersji wywołującej operatora < rozwiązuje problem.

#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

Domyślnie std::less<std::reference_wrapper<const Object>> jest używany, ale nie do przodu operator<() do typu bazowego.

Najprostszym i concisest możliwość rozwiązania problemu jest określenie std::less<const Object> (lub std::greater<const Object>) w definicji mapy tak:

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

będzie działać prawidłowo i zgodnie z oczekiwaniami z powodu implicit conversion of std::reference_wrapper to T& i implicit constructor of std::reference_wrapper.

Example.

Powiązane problemy