我正在尝试使用 pybind11(v2.2.2+) 创建 python 绑定,但无法弄清楚如何调用具有单个 std::initializer_list 参数的 C 函数。
void list_ints(std::initializer_list<int>)
pybind11 绑定是:
m.def("list_ints", &list_ints)
从python,我试图这样调用:
list_ints(1, 2, 3)
这是在 MacOS 上使用 llvm 编译的示例 C 代码-std=C++14
:
#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
using namespace std;
namespace py = pybind11;
void list_ints(const std::initializer_list<int> &il) {
std::cout << "Got to list_ints ..." << std::endl;
for (const auto &elem : il)
std::cout << to_string(elem) << " ";
std::cout << std::endl;
};
PYBIND11_MODULE(initializer, m) {
m.def("list_ints", &list_ints);
m.def("list_ints", (void (*) (const std::initializer_list<int>&)) &list_ints);
# This is the only binding that seems to work .. sort of.
m.def("list_ints", (void (*) (const int &a, const int &b)) &list_ints);
}
python 代码包含对结果的描述:
from initializer import list_ints
try:
# Fails with: TypeError: Incompatible function arguments
print("Calling list_ints(1, 2, 3)")
list_ints(1, 2, 3)
except TypeError as err:
print(err)
# Call succeeds but function Seg Faults!
print("Calling list_ints(1, 2)")
list_ints(1,2)
此测试代码演示了与定义为的参数的绑定const int &a, const int &b
确实匹配并调用了 list_ints 函数,但显然有些事情是不正确的,因为在访问参数时会发生 seg 错误。
$ python initializer.py
Calling list_ints(1, 2, 3)
list_ints(): incompatible function arguments. The following argument types are supported:
1. (arg0: std::initializer_list<int>) -> None
2. (arg0: std::initializer_list<int>) -> None
3. (arg0: int, arg1: int) -> None
Invoked with: 1, 2, 3
Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,
<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic
conversions are optional and require extra headers to be included
when compiling your pybind11 module.
Calling list_ints(1, 2)
Got to list_ints ...
Segmentation fault: 11
有没有办法void list_ints(std::initializer_list<int>)
从 Python 绑定和调用?