Functions
The following example shows a C++ function that takes a std::vector<double> and returns the sum of its elements:
// fun2.cpp
#include <c2py/c2py.hpp>
#include <vector>
#include <numeric>
double sum(std::vector<double> const &v) { return std::accumulate(begin(v), end(v), 0.0); }
We can then generate the Python bindings for this function and compile them with (assuming OS X and clang):
clair-c2py fun2.cpp -- -std=c++20 `c2py_flags -i`
clang++ fun2.cpp -std=c++20 -shared -o fun2.so `c2py_flags`
Finally, we use the module in Python:
>>> import fun2 as M
>>> M.sum([1.2, 2.3, 4.5])
8.0