Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是C++-在Cython中是否可以使用样式内部类型定义? C++中,可以声明类或结构成员的类型别名: struct Foo { // internal type alias typedef int DataType; // ... };_Python_C++_Oop_Cython - Fatal编程技术网

Python 是C++-在Cython中是否可以使用样式内部类型定义? C++中,可以声明类或结构成员的类型别名: struct Foo { // internal type alias typedef int DataType; // ... };

Python 是C++-在Cython中是否可以使用样式内部类型定义? C++中,可以声明类或结构成员的类型别名: struct Foo { // internal type alias typedef int DataType; // ... };,python,c++,oop,cython,Python,C++,Oop,Cython,在Cython有没有办法做同样的事情?我尝试了最明显的方法: cdef struct Foo: ctypedef int DataType 但这不起作用: Error compiling Cython file: ------------------------------------------------------------ ... # distutils: language=c++ cdef struct Foo: ctypedef int DataType

在Cython有没有办法做同样的事情?我尝试了最明显的方法:

cdef struct Foo:
    ctypedef int DataType
但这不起作用:

Error compiling Cython file:
------------------------------------------------------------
...
# distutils: language=c++

cdef struct Foo:
    ctypedef int DataType
   ^
------------------------------------------------------------

internal_typedefs_example.pyx:4:4: Expected an identifier, found 'ctypedef'
这只是Cython的一个基本限制(我使用的是v0.21.2),还是有解决办法


为什么要麻烦内部typedef呢?有几个一般原因——包括其中一些

我感兴趣的特定案例是包装一组模板C++类,看起来类似于:

struct FooDataset
{
    typedef int DataType;
    typedef float ReturnType;

    // methods, other important stuff
};

struct BarDataset
{
    typedef long DataType;
    typedef double ReturnType;

    // methods, other important stuff
};

template <class Dataset>
class DataProcessor{

    DataProcessor(Dataset& input_data);

    typedef typename Dataset::DataType T;
    typedef typename Dataset::ReturnType R;

    T getDataItem();
    R computeSomething(); /* etc. */

    // do some other stuff that might involve T and/or R
};
struct-FooDataset
{
typedef int数据类型;
类型定义浮动返回类型;
//方法,其他重要的东西
};
结构数据集
{
typedef长数据类型;
typedef双返回式;
//方法,其他重要的东西
};
模板
类数据处理器{
数据处理器(数据集和输入数据);
TypeDefTypeName数据集::数据类型T;
TypeDefTypeName数据集::ReturnType R;
T getDataItem();
R computeSomething();/*等*/
//做一些其他可能涉及T和/或R的事情
};
将typedef(s)放在结构内部可以提供更好的封装,因为我只需要传递一个模板参数(
Dataset
类),而不是单独指定特定于该
Dataset
类型的
Dataset
加上
T,R,…


我意识到,在这种情况下找到解决办法并不太难——我最感兴趣的是得到一个明确的答案,即Cython目前是否支持内部TypeDef。

据我所知,Cython目前不支持这一点。但是你不能在结构之外定义它吗

Cython目前不被设计为C++的替代品,而是一种加速Python代码热点的方法。如果你需要更多的参与,只需在C++中编写并公开Python绑定。C++中的

< P>。因此,内部
typedef
可以在Cython中声明为:

cdef cppclass Foo:
    ctypedef int DataType

内部typedef是个好主意的原因有很多-。我将在我的问题中添加更多关于我的特定用例的信息。