简体   繁体   English

为什么没有 arguments 的函数 pybind 会失败?

[英]Why does pybind fail for functions without arguments?

I have an overloaded constructor in C++ (default + other).我在 C++ (默认+其他)中有一个重载的构造函数。 My automatically generated pybind code looks like this:我自动生成的 pybind 代码如下所示:

    py::class_<MyClass>(m, "MyClass")
        .def(
            py::init<>(),
            py::kw_only()
        )
        .def(
            py::init<
                std::valarray<std::string>
            >(),
            py::kw_only(),
            py::arg("my_var")
        )

When I delete the first constructor everything works fine.当我删除第一个构造函数时,一切正常。 But for the first one I get this error:但对于第一个我得到这个错误:

error: static assertion failed: py::kw_only requires the use of argument annotations
static_assert(has_arg_annotations || !has_kw_only_args, 
"py::kw_only requires the use of argument annotations"

Does anyone know why this error is coming up and how to fix it?有谁知道为什么会出现这个错误以及如何解决它?

Edit: I am using a custom pybind generator.编辑:我正在使用自定义 pybind 生成器。

Thanks @463035818_is_not_a_number for pointing me in the right direction.感谢@463035818_is_not_a_number 为我指明了正确的方向。 I was thinking of something like **kwargs, but of course kw_only means, that only named arguments are allowed which doesn't make sense without arguments.我在想像 **kwargs 之类的东西,但当然 kw_only 的意思是,只允许命名为 arguments,如果没有 arguments,这是没有意义的。

In my custom pybind generator I added an if-condition to check whether there are function arguments and only added kw_only if there are any.在我的自定义 pybind 生成器中,我添加了一个 if 条件来检查是否存在 function arguments 并且只添加 kw_only 如果有的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM