超载类成员功能标记为const
Overload class member function marked const
我在标记为 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
标记时,代码可以工作。
我应该如何执行此超载?
const超载方法有一个特殊标签。
namespace py = pybind11;
PYBIND11_MODULE(example,m)
{
py::class_<Foo>(m, "Foo")
.def("bar", py::overload_cast<const std::vector<double>&>(&Foo::bar, py::const_));
}
相关文章:
- muQueue.front() 给出了 const 实例,即使我没有将其标记为 const
- r语言 - C++ 类型为"const std ::?
- 使用共享指针的函数调用,其对象应为 const
- 将参数传递为"const"的奇怪效果
- 如何在声明为 const 的方法中更改类成员
- 将 const 转换为 const char* 无效,我该如何解决?使用 gcc7 时失败
- 当设置为 const 变量时使用 nullptr
- C++ TCP 套接字 | 无法将"std::__cxx11::字符串"转换为"const
- 知道模板参数在编译时是否为 const char*?
- Visual Studio C++:不能使用类型为 "const wchar_t *" 的值来初始化类型为 "TCHAR *" 的实体
- 从 std::string 转换为 const 无符号整数
- 将对象传递给函数而不将其包装到 std::ref 中,而参数被指定为 const 引用
- 将 std::string 转换为 const char* 用于 printf 消耗
- C++如何将 std::vector 转换为 const<int> std::vector<const int>?
- 为什么我需要将默认引用参数定义为 const 以便为其分配一个左值?
- 如何检查函数是否真的获得了定义为 const 的变量?
- 将 std::string 转换为 const tchar*
- STL按客户"<"运算符对向量进行排序。为什么要将"<"运算符定义为 const?
- 无法从 const char* 转换为 const char *&
- 错误:二进制">>":未找到采用类型为"const char [1] 的右操作数的运算符