Fw: [C++-sig] Pyste and STL types
David Abrahams
dave at boost-consulting.com
Wed Mar 19 05:05:28 CET 2003
Nicodemus <nicodemus at globalite.com.br> writes:
> David Abrahams wrote:
>
>>Nicodemus <nicodemus at globalite.com.br> writes:
>>
>>
>>>What do you mean by "active field"? I ask because my knowledge of
>>>unions is limited.
>>>
>>
>>The one someone stored data in.
>>
>
> I know, but from your comment I understood that C/C++ took care of
> this for you, like this:
>
> union Test
> {
> float d;
> int i;
> };
>
> void main()
> {
> Test t;
> t = 3.0; // use the d member
> float x = t; // get the d member
> t = 1; // set the i member
> int z = t; // get the i member
> }
>
> Which I don't think is the case.
I didn't meant that.
> The programmer has to take care of
> what field is active:
Yes.
> void main()
> {
> Test t;
> t.d = 3.0; // set the d member
> int z = t.i; // member i has garbage
> float x = t.d; // ok
> }
>
> What am I missing?
Nothing; I am just suggesting that the Python interface ought to work
like the first example when the union is a member of another class, as
it commonly will be. In order to handle a union coherently there
must be some additional data telling you which field is active, so
the union is commonly packaged in a struct:
struct Test
{
bool is_float;
union val
{
float d;
char* s;
};
};
In Python, we ought to be able to say:
Test.val = 3.14
or
Test.val = 'hello'
...just IMO.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list