• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


27 Mar, 2025

Updated at 20 May, 2025

Cython: specify typed memoryview as template type for C++ map

I'd like to use C++ typed maps instead of Python dicts, where the map's keys are integers and values are typed Cython memoryviews. A minimal (non)working example is:

# distutils: language=c++

from libcpp.unordered_map cimport unordered_map as Map

cdef Map[int, int[:,:]] P;

where I get the compilation error

test.pyx:5:8: Reference-counted type 'int[:, :]' cannot be used as a template argument

Is there any way to specify objects of this type? I'd like to take full advantage of Cython's speed advantages, and that means using typed containers!