我发现了一个overload_cast
失败的具体案例,我不知道如何解决这个问题。
显示该行为的最小示例是
#include <pybind11/pybind11.h>
// ----------------
template<class InputIterator, class OutputIterator>
void absolute(
const InputIterator first, const InputIterator last, const OutputIterator result
)
{
for ( auto it = first ; it != last ; ++it )
*it = std::abs(*it);
}
// ----------------
std::vector<double> absolute(const std::vector<double> &in)
{
std::vector<double> out = in;
for ( auto &i: out )
i = std::abs(i);
return out;
}
// ----------------
namespace py = pybind11;
PYBIND11_MODULE(example,m)
{
m.def("absolute", py::overload_cast<const std::vector<double>&>(&absolute));
}
使用例如编译:
clang++ -O3 -shared -std=c++14 `python3-config --cflags --ldflags --libs` example.cpp -o example.so -fPIC
给出:
example.cpp:32:21: error: no matching function for call to object of type 'const
detail::overload_cast_impl<const vector<double, allocator<double> > &>'
m.def("absolute", py::overload_cast<const std::vector<double>&>(&absolute));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/pybind11/detail/common.h:727:20: note: candidate template ignored: couldn't infer
template argument 'Return'
constexpr auto operator()(Return (*pf)(Args...)) const noexcept
^
/usr/local/include/pybind11/detail/common.h:731:20: note: candidate template ignored: couldn't infer
template argument 'Return'
constexpr auto operator()(Return (Class::*pmf)(Args...), std::false_type = {}) const noexcept
^
/usr/local/include/pybind11/detail/common.h:735:20: note: candidate function template not viable:
requires 2 arguments, but 1 was provided
constexpr auto operator()(Return (Class::*pmf)(Args...) const, std::true_type) const noexcept
^
1 error generated.
删除第一个函数(我不会重载)或更改其签名可以解决问题。但这显然不是我想要的。