如何将constexpr暴露给Cython? [英] How to expose a constexpr to 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屋!