Closed Progeny42 closed 1 year ago
Hi @Progeny42,
Thankfully, directly setting the value via the relevant function (in this example polar_position()) also sets the discriminator.
This is actually the intended way to use unions in this C++ binding. The setter for the discriminator only exists to set the desired discriminant value in a case where multiple values map to the same value. For example, if you have:
enum E { A, B, C, D };
union U switch (E) {
case A: case B:
long x;
case C: case D:
double y;
};
and you want the discriminant value D
with y=3.14
, then the intended way is to do
u.y(3.14); _d(E::D)
It is quite annoying, I agree. I'm glad I didn't define the interface 😂
Ah I see. Thanks for the quick response. I'll go refactor my implementation to suit then.
I'm using tag v0.10.3, and generated the *.idl files using the CXX library with the
idlc
application.For example, a type
position_coordinate_type
is astd::variant
, discrimnated based on an enumeration ofCARTESIAN
,POLAR
, andWGS84
.My understanding of the API is to call the
_d()
function, passing the relevant enumeration, and then use<discriminant_function>()
to set the value.For example:
However, this fails because the setter for the discriminator (
_d()
) performs the following check:m__d
is the_default_discriminator
, which in this example, isCARTESIAN
. Therefore, calling this function with anything butCARTESIAN
will fail, meaning you are unable to change it using this function.I don't believe the check is actually necessary in this function, because the point of the function is to change the discriminant.
Thankfully, directly setting the value via the relevant function (in this example
polar_position()
) also sets the discriminator.