[C++-sig] Conversion between boost::tuple and a python tuple

Colin Irwin colin_irwin at hotmail.com
Tue Jan 25 08:01:29 CET 2005

I have an interface in C++ for which I would like a binding into Python.  In
particular, one of the functions in the interface takes a boost::tuple
argument, e.g.:

class ExampleClass
        void foo( boost::tuple<int, int, int> int_tuple );


I would like to (at least I think I would like to) reflect this into Python
such that a user passes in a Python tuple.

How would one achieve this?  I've searched the BPL documentation and can't
seem to find anything.  I've also searched the relevant newsgroups and there
are some old references (circa 2002) on how to achieve this, but this was
with the v1 of the library, so I don't think the ideas/methods are

Finally, is this a good idea?  By this I mean, would it be a better idea to
change the foo function signature to accept something like a std::vector and
then employ an rvalue converter?  I realise by doing this the intent of the
interface is now a little different, i.e., with above, the user must pass a
tuple of 3 values, whilst using a std::vector could lead to any number of
integers passed into the function.  However, this would make more sense from
the Python side and I suspect would be easy (easier?) to code up - some of
what I can imagine to be difficulties could result from a mismatch in the
python tuple vs. boost::tuple semantics.

All help would be much appreciated.  The BPL is perfect for what I would
like to do, however, I feel I'm lacking a bit of understanding.


More information about the Cplusplus-sig mailing list