Conversion Operators


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

operator std::string() const;

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


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:


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.