[C++-sig] Problem with map_indexing_suite

Damien Dupuis damien.dupuis at lip6.fr
Fri Jun 18 13:23:26 CEST 2010


I'm trying to wrap a whole C++ that contains a lot of access to std::vector and std::map.

I managed to wrap vectors but i've got problems with maps.

The following simple example fails to compile with the error:
error: no match for call to ‘(const boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<X*>) (X*)‘

====================================================
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
using namespace boost::python;

class X {
    public:
    X(std::string s): _s(s) {}
    std::string const repr() { return _s; }

    private:
    std::string _s;
};

class Y {
    public:
    Y(): _vec(), _map() {};
    void addToVec(X* x)                { _vec.push_back(x); }
    void addToMap(int i, X* x)         { _map[i] = x;       }
    const std::vector<X*>& getXVec()   { return _vec;       }
    const std::map<int, X*>& getXMap() { return _map; }

    private:
    std::vector<X*>   _vec;
    std::map<int, X*> _map;
};

BOOST_PYTHON_MODULE(pyTEST) {
    class_<X, X*>("X", init<std::string>())
        .def("__repr__", &X::repr)
    ;

    class_<std::vector<X*> >("XVec")
        .def(vector_indexing_suite<std::vector<X*>, true>())
    ;

    class_<std::map<int, X*> >("XMap")
        .def(map_indexing_suite<std::map<int, X*>, true>())
    ;

    class_<Y>("Y", init<>())
        .def("addToVec", &Y::addToVec)
        .def("addToMap", &Y::addToMap)
        .def("getXVec" , &Y::getXVec , return_internal_reference<>())
        .def("getXMap" , &Y::getXMap , return_internal_reference<>())
    ;
}
====================================================


I tried to use boost::shared_ptr<X> instead of X* in this small example and it works, but I would like not to have to correct all my existing C++ code to use boost::shared_ptr.
And since it works well with std::vector<X*>, I think it should work with maps.

Some google search make me think I should add a return_value_policy to the __getitem__ method of std::map<int, X*> class, but when I try I get the error
error: address of overloaded function with no contextual type information
I'm clearly doing something wrong here.

Thanks for any help/advice.

FYI: I'm using boost 1.42.0, with g++ 4.2.1 on snow leopard (10.6.3)
 
-------------------------------
Damien Dupuis







More information about the Cplusplus-sig mailing list