`overload_cast` 在特定情况下失败

Tom*_*eus 4 c++ pybind11

我发现了一个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));
}
Run Code Online (Sandbox Code Playgroud)

编译使用例如:

clang++ -O3 -shared -std=c++14 `python3-config --cflags --ldflags --libs` example.cpp -o example.so -fPIC
Run Code Online (Sandbox Code Playgroud)

给出:

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.
Run Code Online (Sandbox Code Playgroud)

删除第一个函数(我不重载)或更改其签名可以解决问题。但这显然不是我想要的。

Yak*_*ont 5

using sig = std::vector<double>(const std::vector<double> &in);

py::overload_cast<const std::vector<double>&>((sig*)&absolute)
Run Code Online (Sandbox Code Playgroud)

与 C++ 中的几乎所有其他内容不同,&absolute它不引用实际值或对象。

相反,它指的是过载集;在本例中,是包含特定签名和模板的重载集。

用于推断的模板模式匹配Return失败。

通过转换为特定签名,(sig*)&absolute现在引用一个对象或值——在本例中是指向特定函数的指针。现在模板模式匹配可以推断Return.

在 C++ 中,一般规则是可调用对象没有特定的签名。您可以询问使用特定参数调用的可调用对象返回什么,但询问可调用对象想要使用什么参数进行调用是一种不好的形式。假设可调用对象是 PMF 或函数指针也是不好的形式。

人们一直在解决这个问题,这会导致像您的代码所示的烦人的错误。