在cython中使用函数指针作为模板参数包装C ++代码 [英] Wrapping C++ code with function pointer as template parameter in cython

查看:56
本文介绍了在cython中使用函数指针作为模板参数包装C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在cython中包装以下用C ++编写的声明:

I am trying to wrap the following declaration written in C++ in cython:

template<typename T, double (*distance)(const DataPoint&, const DataPoint&)>
class VpTree
{...}

我在C ++中也有以下定义:

I've also got the following definition in C++:

inline double euclidean_distance(const DataPoint &t1, const DataPoint &t2) {...}

,我正在尝试将其包装在cython中.这是我能够根据文档提出的:

and I'm trying to wrap this in cython. This is what I've been able to come up with following the documentation:

cdef extern from "vptree.h":
    # declaration of DataPoint omitted here

    cdef inline double euclidean_distance(DataPoint&, DataPoint&)

    cdef cppclass VpTree[T, F]:  # F is almost certainly wrong
        ...

并为此构建一个包装器:

and build a wrapper around this:

cdef class VPTree:
    cdef VpTree[DataPoint, euclidean_distance] tree

    def __cinit__(self):
        self.tree = VpTree[DataPoint, euclidean_distance]()

不幸的是,这导致以下错误:

Unfortunately, this results in the following errors:

------------------------------------------------------------

cdef class VPTree:
    cdef VpTree[DataPoint, euclidean_distance] tree
                                            ^
------------------------------------------------------------

unknown type in template argument

------------------------------------------------------------

cdef class VPTree:
    cdef VpTree[DataPoint, euclidean_distance] tree

    def __cinit__(self):
        self.tree = VpTree[DataPoint, euclidean_distance]()
                                     ^
------------------------------------------------------------

unknown type in template argument

我怀疑问题出在定义的 F 部分,我已经尝试了各种方法来代替它,例如 double(*)(DataPoint& ;, DataPoint&),但这显然会导致语法错误.

I suspect the problem lies in the F part of the definition, and I've tried various things in place of that e.g. double(*)(DataPoint&, DataPoint&) but this obviously results in a syntax error.

推荐答案

据我所知,Cython不直接支持非类型模板参数(即函数指针是什么)(尽管我可能错过了备忘录)),但有一个众所周知的 cname-hack 可以实现这一目标.

As far as I know, Cython doesn't support non-type template parameters (that is what function pointer is) directly (I might have missed the memo though), but there is a well known cname-hack to achieve the goal.

在这里,举一个简单得多的例子:

Here, for a much simpler example:

%%cython --cplus            
cdef extern from *:
    """
    template<int val>
    int fun(){return val;}
    """
    int fun[T]()

int -值作为模板参数.

现在我们面临一个难题:Cython期望T为类型,而g ++(或其他编译器)期望为整数值-这是 cname-hack 的代名词:

Now we have a dilemma: Cython expects T to be type and g++ (or other compilers) expects an integer value - here comes the cname-hack to our rescue:

%%cython --cplus            
...
cdef extern from *:
    ctypedef int val2 "2" 

def doit():
    return fun[val2]()

Cython认为 val2 是一种类型( int 的别名),但是在生成的c ++代码(> fun< 2>()),因此c ++编译器会按预期方式看到一个整数值(在这种情况下为 2 ).

Cython believes val2 to be a type (alias for int), but replaces it with 2 in the resulting c++ code (fun<2>()), thus c++-compiler sees an integer-value (2 in this case), as it expects.

对于您的情况,这意味着添加:

For your case that means adding:

%%cython --cplus            
...
cdef extern from *:
    ctypedef int euclidean_distance_t "euclidean_distance" 

cdef class VPTree:
     cdef VpTree[DataPoint, euclidean_distance_t] tree

     def __cinit__(self):
         self.tree = VpTree[DataPoint, euclidean_distance_t]()

如果您在Cython代码中的其他任何地方都没有使用"euclidean_distance",那么您实际上根本不需要包装它.

You actually don't have to wrap "euclidean_distance" at all, if you don't use it anywhere else in Cython code.

这篇关于在cython中使用函数指针作为模板参数包装C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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