Discussion:
[C++-sig] [Boost.Python] Treating boost::variant like a reference?
Mario Lang
2015-08-10 15:00:37 UTC
Permalink
Hi.

I am trying (off and on, because it would be useful, but I am not quite
there yet) to find a way to make Boost.Python cooperate with
Boost.Variant. I sort of managed to make a variant behave like a
value. However, things like modification of a variant inside of a
container doesn't work, because the variant is converted from/to python
everytime it is accessed. Looking at the Boost.Python docs and code, I
think there should be a way to treat a variant like a proxy/reference.
The only problem is, that Boost.Python would *know* the C++ type of the
referenced object only at runtime. While that shouldn't be a problem
for Python,
It looks like Boost.Python doesn't support this. But maybe there is
some way to still acomplish that, and I have missed it?

boost.variant is common enough that it would be worthwhile to have
boost.python support it as best as possible. It took me quite a while
to figure out what I have written below, so, in case there is no better
solution, maybe we want to add something about variants to the
boost.python docs to get people started a little easier?

I've written up what I have found so far here:
http://blind.guru/boost_python-and-boost_variant.html

Any input appreciated.
--
CYa,
⡍⠁⠗⠊⠕
Stefan Ring
2015-08-15 12:16:18 UTC
Permalink
Post by Mario Lang
Hi.
I am trying (off and on, because it would be useful, but I am not quite
there yet) to find a way to make Boost.Python cooperate with
Boost.Variant. I sort of managed to make a variant behave like a
value. However, things like modification of a variant inside of a
container doesn't work, because the variant is converted from/to python
everytime it is accessed. Looking at the Boost.Python docs and code, I
think there should be a way to treat a variant like a proxy/reference.
The only problem is, that Boost.Python would *know* the C++ type of the
referenced object only at runtime. While that shouldn't be a problem
for Python,
It looks like Boost.Python doesn't support this. But maybe there is
some way to still acomplish that, and I have missed it?
boost.variant is common enough that it would be worthwhile to have
boost.python support it as best as possible. It took me quite a while
to figure out what I have written below, so, in case there is no better
solution, maybe we want to add something about variants to the
boost.python docs to get people started a little easier?
http://blind.guru/boost_python-and-boost_variant.html
Any input appreciated.
Interesting question and interesting blog post! Unfortunately, I don't
have an answer. I'm not familiar enough with Boost.Variant, and I have
not had to dig through Boost.Python reference documentation for quite
a while. I have no idea what is the intended level of interoperability
with Boost.Variant.

Loading...