rat*_*ile 3 c++ iostream friend wrapper cython
如何在Cython中包装operator >> overload?
//LIB.h
namespace LIB
{
class Point
{
friend std::istream &operator >> (std::istream &in, Point &pt)
bool operator == (const Point &pos) const
...
}
}
Run Code Online (Sandbox Code Playgroud)
已经声明了一个名称空间namespace "LIB":,那么我该如何处理std :: namespace呢?
#LIB.pxd
cdef extern from "LIB.h" namespace "LIB":
cdef cppclass Point:
#friend std::istream &operator >> (std::istream &in, Point &pt)
bint operator == (const Point &pos) const
...
Run Code Online (Sandbox Code Playgroud)
这里解释了多个cdef extern块是可能的,但我不知道它是如何工作的,因为我无法重新定义类.
我认为最简单的解决方案是假装Cython这operator<<是一种std::istream忘记朋友的方法.然后,C++编译器将对这些部分进行整理.所以这里似乎是一个有效的解决方案(它编译但我并没有一路测试它):
这是我的LIB.h包装文件:
#include <iostream>
namespace LIB {
class Point {
friend std::istream &operator << (std::istream &in, Point);
};
}
Run Code Online (Sandbox Code Playgroud)
Cython包装器应该如下:
cdef extern from "LIB.h" namespace "LIB":
cdef cppclass Point:
pass
cdef extern from "<iostream>" namespace "std":
cdef cppclass istream:
istream &operator << (Point)
istream cin
Run Code Online (Sandbox Code Playgroud)
然后编译器接受以下文件:
cimport lib
def foo():
cdef lib.Point bla
lib.cin << bla
Run Code Online (Sandbox Code Playgroud)
仅供参考,我编译:
cython --cplus bla.pyx
g++ `python-config --cflags` bla.cpp -c
Run Code Online (Sandbox Code Playgroud)