[Cython] New idea for C++ stack allocation
Dag Sverre Seljebotn
dagss at student.matnat.uio.no
Wed May 6 14:23:03 CEST 2009
Robert Bradshaw wrote:
> On Apr 27, 2009, at 11:57 PM, Dag Sverre Seljebotn wrote:
>
>> I got another idea for how using C++ objects without no-arg
>> constructors
>> could work. It means enforcing C++ variable scoping through compile-
>> time
>> errors, but keeping Python syntax.
>
> I think for our first pass at least, we should allocate everything on
> the heap. We could then try to (transparently?) move things to the
> stack when we can (this would have implications for passing things
> around though).
I agree to this.
If I understand you correctly it means that C++ classes would work very
much like cdef classes (like in the wiki proposal).
Some further notes:
Passing things around shouldn't be such a problem; I don't think it
would be much more complicated to do the heap allocation through a
wrapper Python object (it could be opaque at first, only feature would
be type-checking on coercion to a typed C++ class and perhaps store the
C++ class name for its repr).
Conversion of "CppClass" (refcounted wrapper) to "CppClass*" could be
allowed; but probably not the other way (at least without using a
function saying whether ownership of the pointer should be taken over or
not).
All of this *could* have been done for C structs as well, and in C++
there's no real distinction between a struct and a classes. I suppose a
distinction must be done in Cython though because of code like
cdef MyStruct a, b
a.field = ...
b = a # copies value, not reference
(Note that the /only/ difference between the struct keyword and class
keyword in C++ is whether private or public is the default visibility. A
vtable is added if and only if a function is declared as virtual,
whether it is declared as struct or not.)
See also the other discussion on methods in structs.
> What about
>
> cdef CppObject a, b
> a = bar
> b = bar
> del a
> del b
>
> ?
Yes, I thought of that after sending it.
--
Dag Sverre
More information about the Cython-dev
mailing list