Fw: [C++-sig] Pyste and STL types
David Abrahams
dave at boost-consulting.com
Thu Mar 20 01:56:02 CET 2003
Nicodemus <nicodemus at globalite.com.br> writes:
> Now I see what you mean. Indeed, that would be a nice interface.
> One problem that I see is that there's no way to know when the
> "active" member changes in the C++ side, unless the user gives a
> mechanism that allows us to know, like you suggested.
Right.
> But then the user would have to change the wrapping code to update
> this mechanism. Consider this:
>
> struct Test
> {
> bool is_float;
> union
> {
> float d;
> char* s;
> } val;
> };
>
> void use(Test& t)
> {
> t.val.d = 3.0;
> }
>
> There's no way to know that now the active member is "d". The user
> would have to change it to:
>
> void use(Test& t)
> {
> t.val.d = 3.0;
> t.is_float = true;
> }
My point is that if there's no way to know that the active member is
"d", the union is useless anyway, so the user "always" has some member
like is_float which she is keeping in synch to indicate the correct
member to access.
> (unless this is a common practice: like I said before, I know little
> about unions).
I'm saying it's a common practice. How would you use the union
otherwise?
> So I don't think this solution is optimal. Sure, the user could
> provide wrappers for functions like "use", but it's a lot of work
> compared to the gains, in my opinion.
That's debatable. Part of the philosophy of Boost.Python is that you
should be able to wrap C++ code in such a way that it can't be abused
from Python to cause a crash. Some unions are like that naturally,
e.g.:
union { int x; char bytes[sizeof(int)]; };
but others like the union in Test above are not.
> Given this, I think exporting it to python as it is seen in C++ is
> enough. Don't get me wrong: I would gladly implement in Pyste a
> system to export unions like you suggested (I'm not lazy), but I
> don't know if it is possible to do it in a reasonable way for the
> user.
It requires the user to write some extra code. Worth it? I don't
know; I hardly ever use unions myself ;-)
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list