我在重载已标记的类成员函数时遇到const
问题,而未标记函数时没有问题const
。此外,重载本身在纯 C++ 中也能正常工作。
以下失败
#include <vector>
#include <pybind11/pybind11.h>
class Foo
{
public:
Foo(){};
std::vector<double> bar(const std::vector<double> &a) const
{
return a;
}
std::vector<int> bar(const std::vector<int> &a) const
{
return a;
}
};
namespace py = pybind11;
PYBIND11_MODULE(example,m)
{
py::class_<Foo>(m, "Foo")
.def("bar", py::overload_cast<const std::vector<double>&>(&Foo::bar));
}
编译使用:
clang++ -O3 -shared -std=c++14 `python3-config --cflags --ldflags --libs` example.cpp -o example.so -fPIC
给出错误:
...
no matching function for call to object of type 'const detail::overload_cast_impl<const vector<double, allocator<double> > &>'
.def("bar", py::overload_cast<const std::vector<double>&>(&Foo::bar));
...
而当我删除const
函数的标记时代码有效。
我应该如何执行这个重载?