When do type conversions get called?


#1

I have exposed an enumerated type, like this:

typedef enum StatusBits {
    READY = 0x01,
    ERROR = 0x04,
    .
    .
    .etc
} StatusBits;


utility::add_class<StatusBits>(*m, "StatusBits",
{
    { StatusBits::READY, "READY" },
    { StatusBits::ERROR, "ERROR" },
    .
    .
    . etc
});

In my script, I am trying to use the enum as a mask, like this:

def is_ready()
{
    return ( Status() & READY ) == READY;
}

Where Status() is a function that returns an int (that is really a bit pattern).

This gives an error indicating that no appropriate ā€˜&ā€™ operator exists for parameters: (int, StatusBits).

I added a conversion, several different ways:

m->add(chaiscript::type_conversion<const StatusBits, int >());

m->add(chaiscript::type_conversion<const StatusBits, int >([](const StatusBits &t_bt) { return static_cast<int>(t_bt); }));

It seems that these conversions are not getting called, or are wrong.

I can work around the problem by adding:

            m->add(fun([](int n, StatusBits sb) { return static_cast<int>(n & sb); }), "&");

But this answer forces me to add functions for other operators as the need arises, whereas having the conversion function would deal with many use cases that Iā€™m likely to use.

So, I guess the question is, did I do something wrong in my conversion functions, or is the conversion not called in some cases? (What is the best way to handle this?)