-
I'm wrapping a cpp api using pybind11 primarily but also using nanobind after the pybind11 demonstrably works for comparison. In one of the cases which wraps a method, I have a simple cast which works without issues in pybind11 and I am at a loss to make work in nanobind. This is the working m.def("create_interpreter_dsp_factory_from_file", [](const std::string& filename, py::args& args) -> interpreter_dsp_factory* {
std::vector<std::string> params;
std::string error_msg;
std::vector<const char *> argv;
for (const auto &arg : args) {
params.push_back(arg.cast<std::string>());
}
argv.reserve(params.size());
for (auto &i : params)
argv.push_back(const_cast<char *>(i.c_str()));
interpreter_dsp_factory* factory = (interpreter_dsp_factory*)createInterpreterDSPFactoryFromFile(filename, argv.size(), argv.data(), error_msg);
if (!factory) {
std::cerr << "Cannot create factory : " << error_msg;
return NULL;
}
return factory;
}, py::arg("filename"), "Create a Faust DSP factory from a DSP source code as a file.", py::return_value_policy::reference); and in nanobind: m.def("create_interpreter_dsp_factory_from_file", [](const std::string& filename, nb::args& args) -> interpreter_dsp_factory* {
std::vector<std::string> params;
std::string error_msg;
std::vector<const char *> argv;
for (const auto &arg : args) {
params.push_back(arg.cast<std::string>()); // <- THIS DOESN'T WORK
}
argv.reserve(params.size());
for (auto &i : params)
argv.push_back(const_cast<char *>(i.c_str()));
interpreter_dsp_factory* factory = (interpreter_dsp_factory*)createInterpreterDSPFactoryFromFile(filename, argv.size(), argv.data(), error_msg);
if (!factory) {
std::cerr << "Cannot create factory : " << error_msg;
return NULL;
}
return factory;
}, nb::arg("filename"), "Create a Faust DSP factory from a DSP source code as a file.", nb::rv_policy::reference); Does this require a custom caster as per the documentation or is there a simpler solution? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
What does 'this doesn't work' mean? Does it not compile? Does it raise an exception? Are you including the STL string type caster header? |
Beta Was this translation helpful? Give feedback.
Try
nb::cast<std::string>(..)