Skip to content Skip to sidebar Skip to footer

How To Expose A Constexpr To Cython?

A file Globals.h contains the following definition of a constant: namespace MyNameSpace { /** Constants **/ constexpr index none = std::numeric_limits::max(); } ...

Solution 1:

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

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

from mymodule import none

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


Post a Comment for "How To Expose A Constexpr To Cython?"