【发布时间】:2021-12-19 03:39:08
【问题描述】:
我正在使用带有 C++17 标志(高于 C++11 以具有闭包语法)的最新 Cython 到 GCC。 Cython 中的这种 C++ 排序似乎不允许关闭:
# File: myfunc.pyx
from libcpp.vector cimport vector
from libcpp.algorithm cimport sort
# cpdef to call from Python, it just wraps cdef anyway
cpdef myfunc():
# int is just example, I need to sort struct vector
cdef vector[int] v
v.push_back(2)
v.push_back(1)
# Compile error: Expected ')', found 'a'
sort(v.begin(),v.end(), [](int a,int b){
return a<b
})
Cython 是否支持 C++ 闭包以及如何使用它?如何使用闭包进行 C++ 排序,因为我正在将 Python 移植到 Cython,并且有很多 lambda 排序。
【问题讨论】:
-
我搜索了超过 1000 页的 C++ 17 标准文档,但我没有看到任何地方提到的
Cython。为什么您期望“cython”会遵守 C++ 标准? -
对于自 C++11 以来就存在的 C++ 闭包(毫无疑问,在 C++17 中)
-
转到the authors 并询问他们。您是否有在 cython 中使用 lambdas 的当前代码?该问题与
std::sort无关,与不支持您的期望有关。 Lambda 可以在没有 STL 的情况下存在。
标签: python c++ lambda migration cython