[C++-sig] Using boost::tuple from python

Jim Bosch talljimbo at gmail.com
Sat Jun 25 22:06:34 CEST 2011

On 06/25/2011 11:40 AM, Jay Riley wrote:
> I figured I might be able to expose the tuple by some variation on the
> below:
> class_<ActionTargetTuple>("ActionTargetTuple")
> .def("get", &ActionTargetTuple::get<int>,
> return_value_policy<reference_existing_object>())
> ;
> then use get from python, but if it is doable in this way, I'm not sure
> what the set up needs to be. Does anyone know how to do this/could
> suggest an alternative?

tuple::get is templated on the number you want - it doesn't take that as 
an argument.  I don't think there's a way to do this without writing 
your own "get" with a switch statement or a lot of template 
metaprogramming (I don't think get<int> will compile):

object get(ActionTargetTuple & self, int n) {
     switch (n) {
     case 0:
         return object(self.get<0>());
     case 1:
         return object(self.get<1>());
         PyErr_SetString(PyExc_IndexError, "Index out of range");

     .def("get", &get);

I think that's roughly what you need.


More information about the Cplusplus-sig mailing list