Are C++-style internal typedefs possible in Cython?

2024/9/22 22:12:06

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

struct Foo
{// internal type aliastypedef 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.

Answer

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.

https://en.xdnf.cn/q/71887.html

Related Q&A

How do I use a regular expression to match a name?

I am a newbie in Python. I want to write a regular expression for some name checking. My input string can contain a-z, A-Z, 0-9, and _ , but it should start with either a-z or A-Z (not 0-9 and _ ). I…

python - multiprocessing module

Heres what I am trying to accomplish - I have about a million files which I need to parse & append the parsed content to a single file. Since a single process takes ages, this option is out. Not us…

How to make VSCode always run main.py

I am writing my first library in Python, When developing I want my run code button in VS Code to always start running the code from the main.py file in the root directory. I have added a new configurat…

Why does tesseract fail to read text off this simple image?

I have read mountains of posts on pytesseract, but I cannot get it to read text off a dead simple image; It returns an empty string.Here is the image:I have tried scaling it, grayscaling it, and adjust…

python click subcommand unified error handling

In the case where there are command groups and every sub-command may raise exceptions, how can I handle them all together in one place?Given the example below:import click@click.group() def cli():pass…

Data structure for large ranges of consecutive integers?

Suppose you have a large range of consecutive integers in memory, each of which belongs to exactly one category. Two operations must be O(log n): moving a range from one category to another, and findin…

polars slower than numpy?

I was thinking about using polars in place of numpy in a parsing problem where I turn a structured text file into a character table and operate on different columns. However, it seems that polars is ab…

namespace error lxml xpath python

I am transforming word documents to xml to compare them using the following code:word = win32com.client.Dispatch(Word.Application) wd = word.Documents.Open(inFile) # Converts the word infile to xml out…

lark grammar: How does the escaped string regex work?

The lark parser predefines some common terminals, including a string. It is defined as follows:_STRING_INNER: /.*?/ _STRING_ESC_INNER: _STRING_INNER /(?<!\\)(\\\\)*?/ ESCAPED_STRING : "\&quo…

Pycharm unresolved reference on join of os.path

After upgrade pycharm to 2018.1, and upgrade python to 3.6.5, pycharm reports "unresolved reference join". The last version of pycharm doesnt show any warning for the line below:from os.path …