Cython是否可能有C ++风格的内部typedef? [英] Are C++-style internal typedefs possible in Cython?

查看:475
本文介绍了Cython是否可能有C ++风格的内部typedef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,可以声明类或别名成员的类型别名:

  struct Foo 
{
//内部类型别名
typedef int DataType;

// ...
};

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

  cdef struct Foo:
ctypedef int DataType

但这不起作用:

 编译Cython文件时出错:
------------------------------------- -----------------------
...
#distutils:language = c ++

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

internal_typedefs_example.pyx:4:4:需要一个标识符,这是Cython的一个根本限制(我使用的是v0.21.2),或者是Cython的基本限制。是否有解决方法?






为什么要处理内部typedef?有几个一般原因 - 此前的SO问题涵盖了其中的一些。



我感兴趣的特定情况是包装一组模板化的C ++类,看起来像这样:

  struct FooDataset 
{
typedef int DataType;
typedef float ReturnType;

//方法,其他重要的东西
};

struct BarDataset
{
typedef long DataType;
typedef double ReturnType;

//方法,其他重要的东西
};

template< class Dataset>
class DataProcessor {

DataProcessor(Dataset& input_data);

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

T getDataItem();
R computeSomething(); / *等* /

//做其他一些可能涉及T和/或R
}的东西;

struct的内部有typedef赋予我更好的封装,因为我只需要传递一个模板参数( Dataset 类),而不是单独指定 Dataset ... 特定于数据集类型。



我意识到,很难找到这种情况的解决方法 - 我大多只想获得一个确定的答案,关于内部typedef是否目前可能在Cython中。

解决方案

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



Cython目前不是为C ++而设计的,而是一种加快python代码中的热点的方法。如果你需要更多的东西,只需写在C ++中并公开python绑定。


In C++ it's possible to declare type aliases that are members of a class or struct:

struct Foo
{
    // internal type alias
    typedef int DataType;

    // ...
};

Is there any way to do the same thing in Cython? I've tried the most obvious approach:

cdef struct Foo:
    ctypedef int DataType

But this doesn't work:

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

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

internal_typedefs_example.pyx:4:4: Expected an identifier, found 'ctypedef'

Is this just a fundamental limitation of Cython (I'm using v0.21.2), or is there a workaround?


Why bother with internal typedefs? There are several general reasons - this previous SO question covers a few of them.

The specific case I'm interested in is wrapping a set of templated C++ classes that look something like this:

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
};

Having typedef(s) internal to the struct gives me better encapsulation since I only need to pass a single template parameter (the Dataset class) rather than individually specifying the Dataset plus T, R, ... specific to that Dataset type.

I realise that it's not too difficult to find workarounds for this case - I'm mostly just interested in getting a definitive answer as to whether or not internal typedefs are currently possible in Cython.

解决方案

As far as I know this is not currently supported in Cython. But can't you just define it outside of the struct?

Cython is not currently designed as a replacement for C++, rather a way to speed up hot spots in python code. If you need something more involved just write it in C++ and expose python bindings.

这篇关于Cython是否可能有C ++风格的内部typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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