2012-02-04 24 views
6

Tengo un montón de objetos en una jerarquía de clases y me gustaría hacer un std::map usando referencias a esos objetos como las teclas en el mapa. Parece que std::reference_wrapper sería exactamente lo que se necesita para esto, pero parece que no puedo hacerlo funcionar. Lo que he probado hasta ahora:Usando std :: reference_wrapper como la clave en un estándar :: 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]++ 

Sin embargo, siempre se producen errores tanto oscuro del compilador:

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

El error parece estar diciendo que no se puede comparar dos objetos std::reference_wrapper<const Object>, pero parece que debería ser posible: std::reference_wrapper tiene un operador de conversión que puede convertirlo implícitamente a T& (const Object & aquí) y Object tiene operator <, entonces ¿por qué no funciona?

¿Debería funcionar y esto es simplemente un error en g ++? ¿O algo más está pasando?

+0

¿'std :: ref (object) dalle

Respuesta

5

Parece que funcionaría si hiciera que el operador de comparación fuera una función gratuita (que quizás llame a una función de miembro virtual).

Si es una función miembro, a < b realmente significa a.operator<(b); y las conversiones implícitas no se consideran para el argumento del lado izquierdo.

1

En Visual Studio 11 Beta, me sale el mismo problema. El uso de la versión gratuita que llama al operador < resuelve el problema.

#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

Por defecto std::less<std::reference_wrapper<const Object>> se utiliza, pero no lo hace hacia adelante operator<() al tipo subyacente.

La opción más simple y concisest para resolver su problema es definir std::less<const Object> (o std::greater<const Object>) en la definición de un mapa como éste:

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

que funcionará correctamente y como se esperaba, debido a implicit conversion of std::reference_wrapper to T& y implicit constructor of std::reference_wrapper.

Example.

Cuestiones relacionadas