python - 如何在 Cython 中定义 C++ 模板函数类型?

标签 python c++ templates scope cython

A.hpp

// A.hpp
#ifndef RECTANGLE_HPP
#define RECTANGLE_HPP
#include <iostream>
#include <vector>
namespace test {

double euclid_dist(const std::vector<double> & a) {
  return 3.1415926;
}

template <class T, 
          double (*dist)(const std::vector<T> &)>
class foo {
 public:
  foo(std::vector<T> _items) {
   items = _items;
  }  
  ~foo() {} 
  void inside() {
    std::cout << "hello" << std::endl;
  }  
  void func() {
    inside();
  }  
  void prt(std::vector<std::vector<T> > a) {
    if(a.size() >= 1) {
      std::cout << euclid_dist(a[0]) << std::endl;
    }  
  }  
 private:
  std::vector<T> items;
}; // class foo
} // namespace test 
#endif

演示.pyx:

# demo.pyx
# distutils: language = c++
from libcpp.vector cimport vector
cdef extern from "A.hpp" namespace "test":
  double euculid_dist(const vector [double] & a) 

#def test_euclid_dist(const vector [double] a):
#    return euclid_dist(a)

cdef extern from "A.hpp" namespace "test":
  cdef cppclass foo [double, euclid_dist]:
      foo (vector[double]) except +
      void func()
      void prt(vector[vector[double]])

cdef class PyFoo:
    cdef foo *thisptr      # hold a C++ instance which we're wrapping
    def __cinit__(self, vec):
        self.thisptr = new foo(vec)
    def __dealloc__(self):
        del self.thisptr
   def func(self):
        self.thisptr.func()
   def prt(self, d):
        self.thisptr.prt(d)

设置.py

from distutils.core import setup, Extension
from Cython.Build import cythonize

setup(ext_modules = cythonize(Extension(
    "demo",                                # the extesion name
    sources=["demo.pyx"], # the Cython source and additional C++ source files
    language="c++", # generate and compile C++ code
)))

当使用 python setup.py build_ext --inplace 编译时,出现以下错误:

Compiling demo.pyx because it changed.
Cythonizing demo.pyx
running build_ext
building 'demo' extension
x86_64-pc-linux-gnu-g++ -pthread -fPIC -I/usr/include/python2.7 -c         
demo.cpp -o build/temp.linux-x86_64-2.7/rect.o
demo.cpp:551:20: error: ‘euclid_dist’ was not declared in this scope
demo.cpp:551:31: error: template argument 2 is invalid
demo.cpp: In function ‘int __pyx_pf_4rect_5PyFoo___cinit__(__pyx_obj_4rect_PyFoo*, PyObject*)’:
demo.cpp:866:20: error: ‘euclid_dist’ was not declared in this scope
demo.cpp:866:31: error: template argument 2 is invalid
demo.cpp:866:43: error: invalid type in declaration before ‘;’ token
demo.cpp:881:38: error: ‘euclid_dist’ cannot appear in a constant-expression
demo.cpp:881:49: error: template argument 2 is invalid
demo.cpp:881:60: error: cannot convert ‘std::vector<double>’ to ‘int’ in initialization
demo.cpp: In function ‘PyObject* __pyx_pf_4rect_5PyFoo_4func(__pyx_obj_4rect_PyFoo*)’:
demo.cpp:984:26: error: request for member ‘func’ in ‘* __pyx_v_self->__pyx_obj_4rect_PyFoo::thisptr’, which is of non-class type ‘int’
demo.cpp: In function ‘PyObject* __pyx_pf_4rect_5PyFoo_6prt(__pyx_obj_4rect_PyFoo*, PyObject*)’:
demo.cpp:1038:26: error: request for member ‘prt’ in ‘* __pyx_v_self->__pyx_obj_4rect_PyFoo::thisptr’, which is of non-class type ‘int’
error: command 'x86_64-pc-linux-gnu-g++' failed with exit status 1

似乎在范围内找不到euclid_dist 函数,但我认为它在全局范围内..上面的代码有什么问题?谢谢!

最佳答案

Cython 尚不支持非类型模板参数,但对此有一些 hacky 解决方法。

当您创建类的 cython 定义时,您会为其提供显式模板实例化,这样 cython 就不需要知道类的模板化性质。

在你的例子中,foo 的定义是:

cdef extern from "A.hpp" namespace "test":
    cdef cppclass foo "test::foo<double, test::euclid_dist>":
        foo(const vector[double] &) except +
        #... other members

关于python - 如何在 Cython 中定义 C++ 模板函数类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29573666/

相关文章:

python - 如何从 Pandas 数据框中每 7 条记录具有最大值的数据框中获取整行?

c++ - Stack中的无限循环,实现为 "Linked List"

c++ - 在 C++ 中包装算术类型

python - cx_Oracle - DLL 加载失败 : %1 is not a valid Win32 application. python

python - 将 .png 图像转换为 base64

python - 字符串格式错误?

c++ - 无法从具有字符串成员的结构 vector 中读取名称

c++ - 为什么我的简单 C++ GUI 应用程序显示中文消息框?

jquery - 仅使用客户端模板和 Rest 调用的单个 HTML 站点

c++ - 在模板本身中检索最里面的模板类型