python - Cython:嵌套 typedef 的解决方法

标签 python cython

我正在尝试用 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/

相关文章:

python - 更改静态类变量

python - 如何为 anaconda 1.9.1 和 Python 3.3.4 安装 Matplotlib?

python - 使用 py.test 和 coverage.py 覆盖 Cython 模块

python - f2py,返回数组的 Python 函数(向量值函数)

list - Cython - 使用 python 列表初始化向量 [int]

python - CMake 错误 : Unknown CMake command "ocv_glob_modules".“

python - 如何解压pkl文件?

python - Airflow :将 {{ ds }} 作为参数传递给 PostgresOperator

python - 如何利用所有核心来加速基于numpy 3D数组的仿真程序?

python - 二进制 1d Numpy 数组到整数的最快一对一映射