首页 > 解决方案 > 如何在 cython 中声明双 const* const* 变量?

问题描述

我在“example.h”中有一个 c++ 函数:

bool myFunc(double const* const* p);

我想用 cython 代码(在 .pyx 文件中)包装它。但是,当我编写以下代码时:

cdef extern from r"example.h":
    bool myFunc(double const*const* p)

我收到以下错误:

编译 Cython 文件时出错:应为 ')',找到 '*'

并且 pycharm 在 double const* const* p 上显示此错误:

未解决的参考“常量”

我怎样才能声明那种变量?

标签: constantscython

解决方案


在 C/C++ 中,在哪里放置const-qualifier 一直存在争议:要么

void foo(const int *a);

或者

void foo(int const *a);

两者意思相同。

Cython 中没有这样的战斗,因为它只接受第一个版本。

上述规则适用于double**导致:

cdef extern from r"example.h":
    bool myFunc(const double * const* p)

或者作为一种解决方法,可以完全放弃 const 限定符:

cdef extern from r"example.h":
    bool myFunc(const double **p)

建议在大型项目中使用 const-qualifiers 在弄清楚会发生什么时有很大帮助。


推荐阅读