You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pybind11绑定C++构造函数时参数注解不匹配报错原因咨询

pybind11绑定含kwargs构造函数的编译错误问题

报错版本代码

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

namespace py = pybind11;

class PyTestArg {
public:
    PyTestArg(const std::string& vocab, const py::kwargs& kwargs) {
            // 构造函数实现
     }
};

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")
    );
}

编译错误信息

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") );

无报错版本代码

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

namespace py = pybind11;

class PyTestArg {
public:
    PyTestArg(const std::string& vocab, const py::kwargs& kwargs) {
            // 构造函数实现
     }
};

PYBIND11_MODULE(python_example, m) {
    py::class_<PyTestArg>(m, "test")
        .def(py::init<const std::string&, const py::kwargs&>());
}

错误原因

pybind11里的py::kwargs是特殊可变参数类型,不属于普通位置参数范畴,不需要用py::arg标注。你给两个参数都加了注解,导致pybind11计数时认为需要标注的位置参数数量(1个vocab)和你提供的注解数量(2个)不匹配,触发静态断言失败。

修正方案

只给普通位置参数vocab添加注解,去掉kwargs的注解即可:

PYBIND11_MODULE(python_example, m) {
    py::class_<PyTestArg>(m, "test").def(
        py::init<const std::string&, const py::kwargs&>(),
        py::arg("vocab")
    );
}

内容的提问来源于stack exchange,提问作者Damons

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 20:27:01