簡體   English   中英

為什么沒有 arguments 的函數 pybind 會失敗?

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

我在 C++ (默認+其他)中有一個重載的構造函數。 我自動生成的 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")
        )

當我刪除第一個構造函數時,一切正常。 但對於第一個我得到這個錯誤:

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"

有誰知道為什么會出現這個錯誤以及如何解決它?

編輯:我正在使用自定義 pybind 生成器。

感謝@463035818_is_not_a_number 為我指明了正確的方向。 我在想像 **kwargs 之類的東西,但當然 kw_only 的意思是,只允許命名為 arguments,如果沒有 arguments,這是沒有意義的。

在我的自定義 pybind 生成器中,我添加了一個 if 條件來檢查是否存在 function arguments 並且只添加 kw_only 如果有的話。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM