Boost.Python 静态方法重载

发布于 2024-12-25 01:56:10 字数 722 浏览 1 评论 0 原文

如何使用 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:libdistributions 的初始化引发了未报告的异常)。如果我从 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")
;

How do I expose the following class using Boost.Python?

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

I tried something like this:

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

But, it raises an exception in Python (SystemError: initialization of libdistributions raised unreported exception). If I remove one of the overloads from the bp::class_, then everything works fine. I know that Boost.Python can automatically deal with overloaded constructors, so I was wondering if there was something like that for static methods.


Answer

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文