标签: pybind11

重载类标记为 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));
}
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)

给出错误:

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

c++ pybind11

2
推荐指数
1
解决办法
1170
查看次数

如何使用 pybind 访问结构的属性?

我使用 pybind 包装一些 C++ 函数,然后在 Python 中使用它。我需要一些结构,但我不知道如何在 Python 中访问其属性。我的结构没有仅方法属性,所以我认为绑定是这样的(也许这也是错误的):

py::class_<Struct_Sample>(m, "Struct_Sample");
Run Code Online (Sandbox Code Playgroud)

这是结构:

typedef struct Struct_Sample
{
   float time_ms;
   float frequency_mhz;
} Struct_Sample;
Run Code Online (Sandbox Code Playgroud)

如何访问Python中的属性?

c++ python pybind11

2
推荐指数
1
解决办法
3438
查看次数

pybind11::dict 中的 pybind11:: 对象

我尝试将 python 解释器嵌入到我的 C++17 应用程序中。Foo我必须从 python 访问位于 C++ 世界中的的对象实例。

所以我想出了以下代码:

#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <iostream>

namespace py = pybind11;
using namespace py::literals;

class Foo
{
public:
  Foo() : v(42) {}
  int get() const { return v; }
  void set(int x) { v = x; }
private:
  int v;
};

PYBIND11_EMBEDDED_MODULE(my_module, m) {
    py::class_<Foo>(m, "Foo")
      .def(py::init<>())
      .def("get", &Foo::get)
      .def("set", &Foo::set);
}

int main()
{
  py::scoped_interpreter guard{};
  using namespace py::literals;

  py::object py_foo = py::cast(Foo());
  auto locals = py::dict(
    "foo"_a …
Run Code Online (Sandbox Code Playgroud)

c++ python pybind11

2
推荐指数
1
解决办法
2899
查看次数

在 pybind11 和 C++ 中添加源时,没有给出目标错误的源代码

我正在尝试使用 pybind11 使用 C++ 编写的库在 python 中工作。

源文件的编译和构建工作没有错误,但使用 pip 安装时生成的文件会抛出此错误。

 CMake Error at pybind11/tools/pybind11Tools.cmake:131 (add_library):
      Cannot find source file:

        ../project/variables.cpp

      Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
      .hpp .hxx .in .txx
    Call Stack (most recent call first):
      CMakeLists.txt:5 (pybind11_add_module)


    CMake Error at pybind11/tools/pybind11Tools.cmake:131 (add_library):
      No SOURCES given to target: project
    Call Stack (most recent call first):
      CMakeLists.txt:5 (pybind11_add_module)
Run Code Online (Sandbox Code Playgroud)

我正在关注cmake_example并且我确信该文件存在。事实上,如果我删除该文件,运行时就会出现错误

python setup.py sdist
Run Code Online (Sandbox Code Playgroud)

文件夹结构是这样的:

C++ project root folder
|-- C++ …
Run Code Online (Sandbox Code Playgroud)

c++ cmake setuptools pybind11

2
推荐指数
1
解决办法
1万
查看次数

无法在 pybind11 中绑定重载的静态成员函数

我尝试使用 pybind11 绑定静态重载函数,但遇到了一些问题。

\n\n

这是示例代码

\n\n
#include <pybind11/pybind11.h>\n\nnamespace py = pybind11;\n\nclass TESTDB {\n  public:\n    static void aaaa(int a, int b) {printf("aaaaa");};\n    static void aaaa(int a) {printf("xxxxx");};\n};\n\nPYBIND11_MODULE(example, m) {\n\n\n  py::class_<TESTDB>(m, "db")\n     .def_static("aaaa", (void (TESTDB::*)(int, int)) &TESTDB::aaaa);\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但由于以下原因无法编译

\n\n
error: no matches converting function \xe2\x80\x98aaaa\xe2\x80\x99 to type \xe2\x80\x98void (class TESTDB::*)(int, int)\xe2\x80\x99\n   .def_static("aaaa", (void (TESTDB::*)(int, int)) &TESTDB::aaaa);\n note: candidates are: static void TESTDB::aaaa(int)\n static void aaaa(int a) {printf("xxxxx");};\n note:                 static void TESTDB::aaaa(int, int)\n static void aaaa(int a, int b) {printf("aaaaa");};\n
Run Code Online (Sandbox Code Playgroud)\n\n

任何想法? …

c++ python function-pointers pointer-to-member pybind11

2
推荐指数
1
解决办法
2456
查看次数

使用 pybind11 混合类型转换和不透明类型

我正在使用 pybind11 的不透明类型。例如,我为 stl 容器定义了一个std::vector<uint32_t>,它是方法的参数类型FromVec

void FromVec(std::vector<uint32_t> vec);

PYBIND11_MAKE_OPAQUE(std::vector<uint32_t>);

PYBIND11_MODULE(tmap, m)
{
    py::bind_vector<std::vector<uint32_t>>(m, "VectorUint", 
      "Unsigned 32-bit int vector.");

    m.def("from_vec", &FromVec, py::arg("vec")
}
Run Code Online (Sandbox Code Playgroud)

有了这个,我现在可以(在 Python 中)执行以下操作:

vec = VectorUint([2, 66, 262, 662, 26, 62])
from_vec(vec)
Run Code Online (Sandbox Code Playgroud)

但是,类型转换不再起作用,因为该函数现在需要 aVectorUint并且不再接受list,例如:

l = [2, 66, 262, 662, 26, 62]
from_vec(l)
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以同时允许倾斜类型和类型转换?也就是说,当list传递 a 而不是倾斜绑定时VectorUint,它会转换为 astd::vector<uint32_t>而不是 Python 抛出“不兼容的函数参数”?

c++ python pybind11

2
推荐指数
1
解决办法
1485
查看次数

使用 pybind11 绑定数组

我在c中有一个结构如下

typedef struct _person{
         int id[10];
         int number[10];
}person;
Run Code Online (Sandbox Code Playgroud)

如何使用 pybind11 绑定它?

pybind11

2
推荐指数
1
解决办法
2505
查看次数

pybind11 保持对象存活

我正在研究 pybind11 中的一个测试文件,并遇到了keep_alive.

py::keep_alive<1, 2>
py::keep_alive<1, 0>
py::keep_alive<0, 1>
Run Code Online (Sandbox Code Playgroud)

有人可以阐明这个测试文件中的这些用法吗?我知道索引指0的是返回,1指的是this指针。我只能理解py::keep_alive<1, 2>(使用文档),但不能理解它在这个测试文件中的用法。

class Child {
public:
    Child() { py::print("Allocating child."); }
    Child(const Child &) = default;
    Child(Child &&) = default;
    ~Child() { py::print("Releasing child."); }
};
py::class_<Child>(m, "Child")
    .def(py::init<>());

class Parent {
public:
    Parent() { py::print("Allocating parent."); }
    ~Parent() { py::print("Releasing parent."); }
    void addChild(Child *) { }
    Child *returnChild() { return new Child(); }
    Child *returnNullChild() { return nullptr; } …
Run Code Online (Sandbox Code Playgroud)

c++ pybind11

2
推荐指数
1
解决办法
2978
查看次数

Pybind11,如何调用 std::vector 中对象的 __repr__ ?

我正在绑定类型 my_type

py::class_<my_type, std::shared_ptr<my_type>>(m, "MyType")
        .def("__repr__", [](const my_type& o){return fmt::format("MyType: {}", o);});
Run Code Online (Sandbox Code Playgroud)

以及 std::vector<my_type> 与

py::bind_vector<std::vector<my_type>>(m, "MyTypeVector");
Run Code Online (Sandbox Code Playgroud)

如果我希望它的输出是容器中每个对象__repr__的序列,我可以/应该如何在这里声明 MyTypeVector 的方法?MyType.__repr__

pybind11

2
推荐指数
1
解决办法
2788
查看次数

在绑定派生类时,我是否还必须将所有父类绑定到根类?

对或错

要绑定 C++ 类层次结构中的派生类,还必须将所有父类绑定到根类。

我希望在我刚刚开始的项目中绑定一堆自定义数据类型,并希望确定所涉及的工作范围。我正在寻找绑定一个与根类型有 3 个派生级别的类。

当必须绑定父类才能成功绑定子类时,是否有任何经验法则?

pybind11

2
推荐指数
1
解决办法
988
查看次数