包装 C++ Cython 中的模板函数/方法
我正在尝试用 Cython 包装一些 C++ 代码。我有一个使用模板方法的类,但它本身不是模板。
class SomeClass {
template <class T> SomeClass(T& spam);
};
由于该类不是模板而只是构造函数,因此我无法像这样在 Cython 中将该类声明为模板。
# wrong!
cdef extern from "SomeClass.h":
cppclass SomeClass [T]:
SomeClass(T& spam)
如何包装模板方法?
I'm trying to wrap some C++ Code with Cython. I have a class that utilizes a template method, but is not a template itself.
class SomeClass {
template <class T> SomeClass(T& spam);
};
As the class is not a template but only the Constructor, I cannot declare the class as a template in Cython like this.
# wrong!
cdef extern from "SomeClass.h":
cppclass SomeClass [T]:
SomeClass(T& spam)
How can I wrap the template-method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于非构造函数模板方法,使用以下非模板类:
我能够让它工作:
如果您特别需要构造函数模板方法,这可能对您没有帮助,但看起来 Cython 对模板方法的支持确实自最初提出这个问题以来至少略有改善。
For a non-constructor template method, using the following non-template class:
I was able to get this to work:
That may not help you if you specifically need a constructor template method, but it does appear that Cython's support for template methods has improved at least slightly since the time when this question was originally asked.
简单的。 (或者我认为是)在 C++ 类中,成员是模板化的,而在 Cython 中,您声明该类是模板化的。将您的代码更改为:
如果可行,或者:
如果 Cython 支持它。
我不是 cython 专家,所以我可能是错的。
Easy. (Or I think it is) In the C++ class, the member is templated, whereas in Cython, you declare the class to be templated. Change your code either to:
If feasible, or to:
If Cython supports it.
I'm not a cython expert, so I may be wrong.
cython 的 Boost 包装怎么样?
http://www.boost.org/doc/libs /1_54_0/libs/python/doc/index.html
欢迎使用 Boost.Python 第 2 版,这是一个 C++ 库,可实现 C++ 和 Python 编程语言之间的无缝互操作性。新版本从头开始重写,拥有更加方便灵活的界面,以及许多新功能,包括支持:
参考文献和指针
全局注册类型强制
自动跨模块类型转换
高效的函数重载
C++ 到 Python 异常翻译
默认参数
关键字参数
在 C++ 中操作 Python 对象
将 C++ 迭代器导出为 Python 迭代器
文档字符串
我猜你正在寻找类似的东西,它已经作为 boost c++ 库的一部分存在了
How about Boost wrapper for cython?
http://www.boost.org/doc/libs/1_54_0/libs/python/doc/index.html
Welcome to version 2 of Boost.Python, a C++ library which enables seamless interoperability between C++ and the Python programming language. The new version has been rewritten from the ground up, with a more convenient and flexible interface, and many new capabilities, including support for:
References and Pointers
Globally Registered Type Coercions
Automatic Cross-Module Type Conversions
Efficient Function Overloading
C++ to Python Exception Translation
Default Arguments
Keyword Arguments
Manipulating Python objects in C++
Exporting C++ Iterators as Python Iterators
Documentation Strings
I guess you are looking for something like this , it is already there as part of boost c++ library