C++ Template Class with Python Wrapper without Declaring Datatypes

69 views
Skip to first unread message

Jacob Blankenship

unread,
Jan 30, 2021, 2:04:16 AM1/30/21
to cython-users
I have been struggling with how to properly set up a wrapped C++ template class with a Python wrapper. However, I would like to keep the template part where I do not have to declare a datatype. I would also like the convenience of declaring the class as a normal Python object without the "[]". However, would I have to resort to something like the following:

cdef vector[type(a)] //Where a is of unknown type during runtime.

Help would be greatly appreciated. My gut feeling is the answer is not possible due to the compiling aspects. What would be the closest alternative if not the case? Constructors can only be declared once for instance. I would need at least all the basic datatypes.

Thanks,
Jacob

da-woods

unread,
Jan 30, 2021, 9:10:46 AM1/30/21
to cython...@googlegroups.com
There isn't really a good way of doing this.

As you guess, a completely free choice won't (ever) be possible at runtime because you'd have to decide what templates to compile at compile-time.

What you're asking is for something like fused types for classes. It doesn't currently exist and I think there's probably good reasons why nobody has done it yet (i.e. it's harder than it superficially sounds).

The best solution at the moment is to probably to generate a .pyx file containing all the instances you need using something like "{0}"-style string formatting. But that isn't hugely satisfactory really.
--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/8b584e85-6475-474f-a5d9-09b882147780n%40googlegroups.com.


Reply all
Reply to author
Forward
0 new messages