Stefan Behnel wrote:
> Dag Sverre Seljebotn, 07.07.2010 08:49:
>   
>> Robert Bradshaw wrote:
>>     
>>> The primary goal with C++ support is to provide just enough C++isms to
>>> easily use any of the many C++ libraries out there, but not so much
>>> that the user can easily hang themselves. The goal is not to
>>> facilitate writing C++ in Cython, but rather using C++ from Cython
>>> (though the two overlap quite a bit on the technical plane). If we
>>> want to disallow, say, references in function arguments, now is the
>>> time to do so. Is there any reason this is necessary to allow?
>>>
>>>       
>> I don't think anything of this has been on writing C++, just about
>> talking to C++ libraries.
>>
>> The reason to allow references in function arguments in "cdef extern"
>> C++ functions is that, well, libraries do come with such constructs, and
>> it is not clear how one would communicate with them if references in
>> function arguments isn't supported somehow. They can be used either as
>> out arguments, or for passing around some global context, etc.
>>     
>
> With my limited knowledge of C++, I don't see a problem here. After all, 
> Cython ignores the exact values stored in those references, and as longs as 
> you can't pass a ref-counted Python object as a C++ reference, it doesn't 
> have to know anything about them.
>
> This will not work, though:
>
>      function_taking_a_reference(<PyObject*>some_object)
>
> but it should not be too hard to catch by the compiler, in case it's 
> currently valid.
>   
Hmm. Not sure if I follow you. Just to make sure everybody's on the same 
page, I'll put down what C++ references is, just in case.

They're syntax candy around const pointers. A reference is a) a pointer, 
b) always points to something, c) never change to point to something 
else, d) is always automatically dereferenced (so assignment etc. acts 
on the pointed-to object).

int a = 4;
int &b = a;
b = 3; // changes a to 3. There's no way to change the pointer b itself.

The current state in Cython leads to the following problem:

C++:
void f(int& x) { x = 10; }

Cython:
cdef extern from "foo.h":
    void f(int x) # int* will cause syntax errors, it must be plain int

cdef int x = 3
y = 3 # object
f(x) # x changes to 10
f(y) # the temporary result of conversion is changed -- y is not changed!

So the current state is both inobvious and would lead to hard to find 
bugs IMO. So that's why I argue explicit support is needed in some form 
or another.

Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to