Conversion Operators


#1

Are there conversion operators? I’m trying to export a C++ class to ChaiScript and I’m not seeing what to name the operator. For example:

class Test
{
public:

operator std::string() const;
};

What do I name the function when adding to the ChaiScript VM?


#2

You want to use the type_conversion functionality to make chaiscript aware of a user defined type conversion. You can use add_type_conversion from within ChaiScript:

Or chai.add(type_conversion...) shown here:

and here:

The signature you almost certainly want is the version that applies type conversions that C++ knows about, basically:

chai.add(type_conversion<Test, std::string>());

This version will fail to compile if C++ does not find a conversion. In your example above, it should find a conversion and should work.

See also:


#3

Oh, @PlayDough one final note, if you get any build errors, you might need to use the latest develop code, I fixed a minor error that made it into the 5.5.0 release.