Boost.Python 静态方法重载

8 投票
1 回答
4551 浏览
提问于 2025-04-17 09:28

我想知道如何使用Boost.Python来暴露以下这个类。

class C {
 public:
  static void F(int) {}
  static void F(double) {}
};

我尝试了类似这样的代码:

bp::class_<C>("C")
  .def("F", (void (C::*)(int))&C::F).staticmethod("F")
  .def("F", (void (C::*)(double))&C::F).staticmethod("F")
;

但是,这在Python中引发了一个异常(SystemError: initialization of libdistributions raised unreported exception)。如果我从bp::class_中移除一个重载的构造函数,那么一切就正常了。我知道Boost.Python可以自动处理重载的构造函数,所以我在想,是否也有类似的方式可以处理静态方法。


回答

bp::class_<C>("C")
  .def("F", (void (C::*)(int))&C::F)  // Note missing staticmethod call!
  .def("F", (void (C::*)(double))&C::F).staticmethod("F")
;

1 个回答

撰写回答