我正在尝试用 Cython 包装一些 C++ 代码。
c++ 头文件“graph.h”包含以下定义:
#include "Block.h"
template <typename graphtype> class Graph
{
public:
typedef enum
{
SOURCE = 0,
SINK = 1
} termtype; // terminals
typedef int node_id;
typedef Block<double> Block_D;
Graph()
}
我在“Graph.pyx”中尝试了以下内容:
cdef extern from "graph.h":
cdef cppclass Graph[graphtype]:
ctypedef int node_id
ctypedef enum termtype:
SOURCE
SINK
ctypedef Block<double> Block;
但是,这些都不起作用。我发现 Cython 可能不支持嵌套的 typedef。如果是这样,是否有解决此问题的方法?
谢谢!
最佳答案
我已经使用 namespace
关键字获得嵌套定义,指定嵌套声明。就像,如果你有名为 mystuff.hpp
的 C++ header 中的以下内容:
namespace MyStuff {
struct Outer {
struct Inner {
int value;
};
Inner member;
};
}
……你可以像这样对这些结构进行 encython:
cdef extern from "mystuff.hpp" namespace "MyStuff::Outer":
cppclass Inner:
int value
cdef extern from "mystuff.hpp" namespace "MyStuff":
cppclass Outer:
Inner member
…如果你真的把 C++ 领域的所有东西都包裹在一个命名空间中,它读起来会更连贯(否则第二个 cdef
在它的声明中没有 namespace
,在我看来,这看起来更奇怪)。
我有许多现实世界中当前正在使用的示例:one such example is here , another one is here .插图示例代码最初显示为 here .
关于python - Cython:嵌套 typedef 的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37185264/