"using Time = cppClassDefinition<withT>" 的 Cython 等价物

Cython equivalent for "using Time = cppClassDefinition<withT>"

我正在尝试包装一个 cpp 应用程序,源代码在头文件中包含以下内容

using Time = cppClassDefinition<withT>

...

void setDefaultTime(Time x)

与此对应的 Cython 是什么?

我试过了

cdef extern from "headerfile.h" namespace "ns":
    ctypedef cppClassDefinition<withT> Time

没有成功。虽然 Cython 在这一步没有报错,但当我尝试使用函数 setDefaultTime(1.0) 时它会抛出编译错误。错误指出“无法将类型 'double' 分配给 'Time'。但是,在 CPP 代码中,这似乎工作正常。

我也试过了

cdef extern from "headerfile.h" namespace "ns":
    cdef cppclass Time:
        pass

那也失败了。有什么建议么?这可能使用 Cython 吗?

正如您在问题中所建议的那样,您应该可以使用 use

ctypedef cppClassDefinition[withT] Time

因为 using ... = ... 在此上下文中等同于 typedef。 (请注意与您问题中的代码相比方括号的变化)。

我认为问题出在您尝试 setDefaultTime(1.0) 上。 Cython 无法知道 double 可以转换为 Time,也无法告诉它隐式 C++ 转换。

最简单的方法就是告诉 Cython 函数签名是

void setDefaultTime(double x)

(您可以保留 C++ 签名)。这将满足 Cython,然后它生成的 C++ 代码应该最终正常工作,前提是 double 可以隐式转换为 Time(正如问题所暗示的)