英文:
error happened when bind cpp code with pybind11
问题
第一段代码中的错误是由于在py::class_中的 .def() 函数中提供了参数名 py::arg("vocab") 和 py::arg("kwargs"),但是没有在构造函数 py::init<> 的参数列表中匹配这些参数名。这导致了参数注解的数量不匹配,从而触发了错误。
第二段代码中,你只是简单地调用了 py::init<>,没有提供参数名,因此没有参数注解的数量不匹配问题,所以没有错误。
要解决第一段代码的问题,你可以在 py::init<> 的参数列表中使用相同的参数名,以使参数注解匹配,就像第二段代码中那样。这将解决错误并让代码正常工作。
英文:
I try to bind code with pybind11 like this:
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
class PyTestArg {
public:
PyTestArg(const std::string& vocab, const py::kwargs& kwargs) {
// Constructor implementation
}
};
PYBIND11_MODULE(python_example, m) {
py::class_<PyTestArg>(m, "test").def(
py::init<const std::string&,
const py::kwargs&>(),
py::arg("vocab"),
py::arg("kwargs")
);
}
However, this always produces the error below, even though I thought I have passed in the correct arguments:
In file included from src/main.cpp:1:
/private/var/folders/j6/cd1wt3mn0xx0hjc9xc63qfqr0000gp/T/pip-build-env-u_92z2qj/overlay/lib/python3.10/site-packages/pybind11/include/pybind11/pybind11.h:217:9: error: static_assert failed due to requirement 'expected_num_args<pybind11::name, pybind11::is_method, pybind11::sibling, pybind11::detail::is_new_style_constructor, pybind11::arg, pybind11::arg>(sizeof...(Args), argument_loader<pybind11::detail::value_and_holder &, const std::string &, const pybind11::kwargs &>::args_pos >= 0, argument_loader<pybind11::detail::value_and_holder &, const std::string &, const pybind11::kwargs &>::has_kwargs)' "The number of argument annotations does not match the number of function arguments"
static_assert(
^
/private/var/folders/j6/cd1wt3mn0xx0hjc9xc63qfqr0000gp/T/pip-build-env-u_92z2qj/overlay/lib/python3.10/site-packages/pybind11/include/pybind11/pybind11.h:101:9: note: in instantiation of function template specialization 'pybind11::cpp_function::initialize<(lambda at /private/var/folders/j6/cd1wt3mn0xx0hjc9xc63qfqr0000gp/T/pip-build-env-u_92z2qj/overlay/lib/python3.10/site-packages/pybind11/include/pybind11/detail/init.h:205:13), void, pybind11::detail::value_and_holder &, const std::string &, const pybind11::kwargs &, pybind11::name, pybind11::is_method, pybind11::sibling, pybind11::detail::is_new_style_constructor, pybind11::arg, pybind11::arg>' requested here
initialize(
^
/private/var/folders/j6/cd1wt3mn0xx0hjc9xc63qfqr0000gp/T/pip-build-env-u_92z2qj/overlay/lib/python3.10/site-packages/pybind11/include/pybind11/pybind11.h:1568:22: note: in instantiation of function template specialization 'pybind11::cpp_function::cpp_function<(lambda at /private/var/folders/j6/cd1wt3mn0xx0hjc9xc63qfqr0000gp/T/pip-build-env-u_92z2qj/overlay/lib/python3.10/site-packages/pybind11/include/pybind11/detail/init.h:205:13), pybind11::name, pybind11::is_method, pybind11::sibling, pybind11::detail::is_new_style_constructor, pybind11::arg, pybind11::arg, void>' requested here
cpp_function cf(method_adaptor<type>(std::forward<Func>(f)),
^
/private/var/folders/j6/cd1wt3mn0xx0hjc9xc63qfqr0000gp/T/pip-build-env-u_92z2qj/overlay/lib/python3.10/site-packages/pybind11/include/pybind11/detail/init.h:203:12: note: in instantiation of function template specialization 'pybind11::class_<PyTestArg>::def<(lambda at /private/var/folders/j6/cd1wt3mn0xx0hjc9xc63qfqr0000gp/T/pip-build-env-u_92z2qj/overlay/lib/python3.10/site-packages/pybind11/include/pybind11/detail/init.h:205:13), pybind11::detail::is_new_style_constructor, pybind11::arg, pybind11::arg>' requested here
cl.def(
^
/private/var/folders/j6/cd1wt3mn0xx0hjc9xc63qfqr0000gp/T/pip-build-env-u_92z2qj/overlay/lib/python3.10/site-packages/pybind11/include/pybind11/pybind11.h:1606:14: note: in instantiation of function template specialization 'pybind11::detail::initimpl::constructor<const std::string &, const pybind11::kwargs &>::execute<pybind11::class_<PyTestArg>, pybind11::arg, pybind11::arg, 0>' requested here
init.execute(*this, extra...);
^
src/main.cpp:115:10: note: in instantiation of function template specialization 'pybind11::class_<PyTestArg>::def<const std::string &, const pybind11::kwargs &, pybind11::arg, pybind11::arg>' requested here
.def(py::init<const std::string&, const py::kwargs&>(), py::arg("vocab") , py::arg("kwargs") );
What causes this error?
When I write the code like this, there is no error:
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
class PyTestArg {
public:
PyTestArg(const std::string& vocab, const py::kwargs& kwargs) {
// Constructor implementation
}
};
PYBIND11_MODULE(python_example, m) {
py::class_<PyTestArg>(m, "test")
.def(py::init<const std::string&, const py::kwargs&>());
}
答案1
得分: 1
以下是翻译好的部分:
"你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。以下是要翻译的内容:
Well you haven't actually given them default values yet, try:
.def(py::init<const std::string&, const py::kwargs&>(), py::arg("vocab") = "");
Secondly you will notice I have missed out the py::arg() tag. This is because the documentation says it should not be included:
https://pybind11.readthedocs.io/en/stable/advanced/functions.html#accepting-args-and-kwargs
Alternatively you could use: py::kw_only()
https://pybind11.readthedocs.io/en/stable/advanced/functions.html#keyword-only-arguments"
英文:
Well you haven't actually given them default values yet, try:
.def(py::init<const std::string&, const py::kwargs&>(), py::arg("vocab") = "");
Secondly you will notice I have missed out the py::arg()
tag. This is because the documentation says it should not be included:
https://pybind11.readthedocs.io/en/stable/advanced/functions.html#accepting-args-and-kwargs
Alternatively you could use: py::kw_only()
https://pybind11.readthedocs.io/en/stable/advanced/functions.html#keyword-only-arguments
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论