[C++-sig] Exposing members of members

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Tue Jan 20 00:13:28 CET 2009

I'd define to/from Python conversions for the std::pair, mapping to
Python tuples, then use .add_property().

Alternatively, wrap std::pair the normal way via class_, then you
can use .def_readwrite().

You need to handle each std::pair<A,B> combination of A,B separately,
like any other template class you want to wrap or convert to/from a
Python object.

See also:


This could be a starting point for std::pair<A,B> conversions. Probably you want
to make it into a converter template if you have more than one combination of A,B.
Here is code we use for the to_python conversions:


We don't have corresponding from_python conversions.
If you add them, let me know!

The next item in the FAQ is also relevant.

----- Original Message ----
From: Ricardo Abreu <gumbeto at gmail.com>
To: cplusplus-sig at python.org
Sent: Monday, January 19, 2009 7:27:46 AM
Subject: [C++-sig]  Exposing members of members


Suppose that I have a class A with a member of type std::pair, called a. If I want to expose class A to python while allowing read/write access to the object pointed to by a.first, what to I put in def_readwrite?

I tried something like class<A>("A").def_readwrite("a", &A::a::first); but it doesn't work. Also been trying with boost::bind but didn't have any luck :s

I know I could do getters and setters, but there would be a lot of them because my "A" has a lot of members like "a".

Thank you in advance,

More information about the Cplusplus-sig mailing list