それほど難しくないはずだが、実際バグらせると無限のコンパイルエラーが出て結構つらい。以下はサンプルコード。
g++ -O0 -Wall -std=gnu++2a -I/usr/include `python3 -m pybind11 --includes` `python3-config --ldflags` test.cpp -o pytest
#include <iostream> #include <vector> #include <typeinfo> #include <pybind11/pybind11.h> #include <pybind11/embed.h> #include <pybind11/stl.h> namespace py = pybind11; int main() { // インタプリタを埋め込まないとsegmentation faultになる py::scoped_interpreter python{}; py::object empty = py::module::import("numpy").attr("empty"); auto arr = empty(std::vector<std::size_t>{3, 3}); std::cout << typeid(arr).name() << std::endl; py::print(arr); py::detail::any_container<ssize_t> shape{{3, 3}}; py::array_t<double> arr2{shape}; std::cout << typeid(arr).name() << std::endl; py::print(arr2); return 0; }
N8pybind116objectE [[6.92735986e-310 4.64273590e-310 0.00000000e+000] [0.00000000e+000 0.00000000e+000 0.00000000e+000] [0.00000000e+000 0.00000000e+000 0.00000000e+000]] N8pybind117array_tIdLi16EEE [[0.00000000e+000 8.19690546e+247 6.01346953e-154 7.13445852e+159] [9.50457424e-259 1.49005471e+195 8.42959463e+252 4.73833000e+170] [3.67147305e+228 2.11997445e+161 3.12577052e-085 2.56930068e+151] [6.01347002e-154 3.94527705e-114 8.81493661e-106 5.16386450e-109]]