绑定cpp代码与pybind11时发生了错误。

huangapple go评论71阅读模式
英文:

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 &lt;pybind11/pybind11.h&gt;
#include &lt;pybind11/stl.h&gt;
    
namespace py = pybind11;
    
class PyTestArg {
public:
    PyTestArg(const std::string&amp; vocab, const py::kwargs&amp; kwargs) {
            // Constructor implementation
     }
};
    
PYBIND11_MODULE(python_example, m) {
    py::class_&lt;PyTestArg&gt;(m, &quot;test&quot;).def(
     py::init&lt;const std::string&amp;, 
     const py::kwargs&amp;&gt;(),
     py::arg(&quot;vocab&quot;), 
     py::arg(&quot;kwargs&quot;)
    );
}

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 &#39;expected_num_args&lt;pybind11::name, pybind11::is_method, pybind11::sibling, pybind11::detail::is_new_style_constructor, pybind11::arg, pybind11::arg&gt;(sizeof...(Args), argument_loader&lt;pybind11::detail::value_and_holder &amp;, const std::string &amp;, const pybind11::kwargs &amp;&gt;::args_pos &gt;= 0, argument_loader&lt;pybind11::detail::value_and_holder &amp;, const std::string &amp;, const pybind11::kwargs &amp;&gt;::has_kwargs)&#39; &quot;The number of argument annotations does not match the number of function arguments&quot;
              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 &#39;pybind11::cpp_function::initialize&lt;(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 &amp;, const std::string &amp;, const pybind11::kwargs &amp;, pybind11::name, pybind11::is_method, pybind11::sibling, pybind11::detail::is_new_style_constructor, pybind11::arg, pybind11::arg&gt;&#39; 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 &#39;pybind11::cpp_function::cpp_function&lt;(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&gt;&#39; requested here
              cpp_function cf(method_adaptor&lt;type&gt;(std::forward&lt;Func&gt;(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 &#39;pybind11::class_&lt;PyTestArg&gt;::def&lt;(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&gt;&#39; 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 &#39;pybind11::detail::initimpl::constructor&lt;const std::string &amp;, const pybind11::kwargs &amp;&gt;::execute&lt;pybind11::class_&lt;PyTestArg&gt;, pybind11::arg, pybind11::arg, 0&gt;&#39; requested here
              init.execute(*this, extra...);
                   ^
      src/main.cpp:115:10: note: in instantiation of function template specialization &#39;pybind11::class_&lt;PyTestArg&gt;::def&lt;const std::string &amp;, const pybind11::kwargs &amp;, pybind11::arg, pybind11::arg&gt;&#39; requested here
              .def(py::init&lt;const std::string&amp;, const py::kwargs&amp;&gt;(), py::arg(&quot;vocab&quot;) , py::arg(&quot;kwargs&quot;) );

What causes this error?

When I write the code like this, there is no error:

#include &lt;pybind11/pybind11.h&gt;
    #include &lt;pybind11/stl.h&gt;
    
    namespace py = pybind11;
    
    class PyTestArg {
    public:
        PyTestArg(const std::string&amp; vocab, const py::kwargs&amp; kwargs) {
            // Constructor implementation
        }
    };
    
    PYBIND11_MODULE(python_example, m) {
        py::class_&lt;PyTestArg&gt;(m, &quot;test&quot;)
            .def(py::init&lt;const std::string&amp;, const py::kwargs&amp;&gt;());
    }

答案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&lt;const std::string&amp;, const py::kwargs&amp;&gt;(), py::arg(&quot;vocab&quot;) = &quot;&quot;);

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

huangapple
  • 本文由 发表于 2023年5月25日 11:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定