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

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


这是示例代码


#include <pybind11/pybind11.h>


namespace py = pybind11;


class TESTDB {

  public:

    static void aaaa(int a, int b) {printf("aaaaa");};

    static void aaaa(int a) {printf("xxxxx");};

};


PYBIND11_MODULE(example, m) {



  py::class_<TESTDB>(m, "db")

     .def_static("aaaa", (void (TESTDB::*)(int, int)) &TESTDB::aaaa);


}

但由于编译失败


error: no matches converting function ‘aaaa’ to type ‘void (class TESTDB::*)(int, int)’

   .def_static("aaaa", (void (TESTDB::*)(int, int)) &TESTDB::aaaa);

 note: candidates are: static void TESTDB::aaaa(int)

 static void aaaa(int a) {printf("xxxxx");};

 note:                 static void TESTDB::aaaa(int, int)

 static void aaaa(int a, int b) {printf("aaaaa");};

任何的想法?


德玛西亚99
浏览 331回答 2
2回答

UYOU

问题是你的演员(void (TESTDB::*)(int, int))。该转换将指向静态成员函数的指针转换为指向非静态成员函数的指针,这是不正确的。由于函数是静态的,您应该简单地将它们转换为指向普通非成员函数的指针:py::class_<TESTDB>(m,&nbsp;"db") &nbsp;&nbsp;&nbsp;&nbsp;.def_static("aaaa",&nbsp;static_cast<void&nbsp;(*)(int,&nbsp;int)>(&TESTDB::aaaa));

摇曳的蔷薇

仅供参考 - 首先,C++ 标准草案n3337(本质上是 C++11)的8.3.1 指针 [dcl.ptr] / 1在声明 TD 中,其中 D 具有以下形式*&nbsp;attribute-specifier-seq&nbsp;cv-qualifier-seq&nbsp;D1并且声明 T D1 中的标识符的类型是“&nbsp;derived-declarator-type-list&nbsp;T”,那么 D 的标识符的类型是“&nbsp;derived-declarator-type-list&nbsp;cv-qualifier-seq指向 T 的指针”。...和8.3.3 成员指针 [dcl.mptr] / 1状态在声明 TD 中,其中 D 具有以下形式nested-name-specifier&nbsp;*&nbsp;attribute-specifier-seq&nbsp;cv-qualifier-seq&nbsp;D1并且nested-name-specifier表示一个类,声明T D1中标识符的类型是“&nbsp;derived-declarator-type-list&nbsp;T”,那么D的标识符的类型是“&nbsp;derived-declarator-type-”列出&nbsp;指向T 类型嵌套名称说明符类成员的cv-qualifier-seq指针。...这些语句意味着我们必须在当且仅当函数是成员函数时使用上面的嵌套名称说明符&nbsp;TESTDB::TESTDB::*TESTDB::aaaa。接下来,5.2.2 函数调用【expr.call】状态函数调用有两种:普通函数调用和成员函数63(9.3)调用。...在脚注63是63&nbsp;) 静态成员函数 (9.4) 是一个普通函数。这意味着您的静态成员函数TESTDB::aaaa是一个普通函数,而不是一个成员函数。因此,您不得TESTDB::在当前演员表中指定。总之,您必须消除TESTDB::aaaa像这样的两个重载的歧义:现场演示static_cast<void&nbsp;(*)(int,&nbsp;int)>(&TESTDB::aaaa)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python