如何将constexpr暴露给Cython? [英] How to expose a constexpr to Cython?

查看:96
本文介绍了如何将constexpr暴露给Cython?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件 Globals.h 包含以下常量定义:

A file Globals.h contains the following definition of a constant:

namespace MyNameSpace {

/** Constants **/
constexpr index none = std::numeric_limits<index>::max();

}

...其中 index uint64_t 的typedef。

... where index is a typedef for uint64_t.

如何将其公开给Cython和Python?

How can I expose it to Cython and Python?

尝试失败:

cdef extern from "../cpp/Globals.h" namespace "MyNamespace":
    cdef index _none "MyNamespace::none"

none = _none


推荐答案

公开(全局)常量的语法与公开简单属性的语法公开静态成员的语法,在您的示例中,语法几乎是正确的,只是您需要省略 cdef 语句, cdef 语句仅用于在Cython中声明新变量,而不用于添加有关外部声明的变量的信息。

The syntax for exposing (global) constants is similar to the syntax for exposing simple attributes and the syntax for exposing static members, in your example the syntax is almost right except that you need to omit the cdef statement, the cdef statement is only for declaring new variables in Cython, not for adding information about externally declared variables.

cdef extern from "../cpp/Globals.h" namespace "MyNamespace":
    index _none "MyNamespace::none"

none = _none

然后可以使用 none 在Python中,如果您的Cython模块名为 mymodule ,则导入语句可能为

Then you can use none in Python, if your Cython module is named mymodule, the import statement could be

from mymodule import none

如果要使 none 在您的Python代码中可用作全局名称。

if you want to make none available as global name in your Python code.

这篇关于如何将constexpr暴露给Cython?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆