[C++-sig] map key

Mike Rovner mike at bindkey.com
Fri Jan 17 22:39:53 CET 2003


I'm trying to wrap a std::map<const Key*, Value>  with

template<class T>
struct map_item

{

typedef typename T::key_type Key;

typedef typename T::mapped_type Value;


static Value const& get(T& x, const Key n)

{

if( x.find(n) != x.end() ) return x[n];

PyErr_SetString(PyExc_KeyError,"Map key not found");
throw_error_already_set();

}


static void set(T& x, const Key n, const Value& val) { x[n]=val; }


static void del(T& x, const Key n) { x.erase(n); }


static bool in(T const& x, const Key n) { return x.find(n) != x.end(); }


static list keys(T const& x)

{ list t;

for(T::const_iterator it=x.begin(); it!=x.end(); ++it)

t.append( object(it->first) );

return t;

}

static list values(T const& x)

{ list t;

for(T::const_iterator it=x.begin(); it!=x.end(); ++it)

t.append( object(it->second) );

return t;

}

static list items(T const& x)

{ list t;

for(T::const_iterator it=x.begin(); it!=x.end(); ++it)

t.append( make_tuple(object(it->first), object(it->second)) );

return t;

}

};

typedef std::map<const Key*, Value> Map;
class_<Key, boost::noncopyable>("Key", no_init) /*...*/;

class_<Value>("Value") /*...*/;

class_<Map>("Map")

.def("keys", &map_item<Map>::keys)

;

My problem is to return keys:

>>> m=Map(...)

>>> m.keys()

TypeError: No to_python (by-value) converter found for C++ type: class Key

Other functions returning const Key* are just fine with
return_internal_reference.

Can I specify return_internal_reference when constructing keys list? How?
Will it solve the problem?

Thanks in advance,

Mike







More information about the Cplusplus-sig mailing list