>> 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.
>>
>> Dag Sverre


Sorry for the partially written mail above. My fingers ripped...

I actually wanted to say I don't get why it is important for Cython to
know anything about C++ references. But then I saw the post of Dag
below and saw his point. I will still comment on why I don't think
specific support for C++ references is that important in my (short)
experience (just my 2 cents, please do not flame me if you think
otherwise or that I did not see some obvious cases :-) .

I am currently wrapping several small C++ libraries/programs, and I
found that I can basically do everything without declaring references
in Cython:

If a C++ function use references as arguments, I just use  the plain
type in the Cython declaration. (for example, "void push_back(T)"
instead of "void push_back(&T)" in a wrapping of std::vector).  This
also protects me from bug #550.

I do the same for functions returning an reference (i.e the declared
return value is the plain type, not a reference). And when a function
returning a reference needs to be a l-value, I use the following
templated function declared in "my_cpp_utils.h":
"""""""
template<typename T,typename V>
void assign(T &target, const V &value){ target=value;}
"""""""

Thus, a cython code using std::vector would be like this:
""""""""
cdef extern from "<vector>":
   cdef cppclass vector[T]:
         void push_back(T)
         T at(size_t)

cdef extern from "my_cpp_utils.h":
       void assign(int,int)

vector[int] vect(10)
assign(vect.at(4),56)
vect.push_back(30)
""""""""
No reference ever declared... The only problem is that I have to add a
declaration for "assign" for every type combination I need to use,
since templated functions are not supported by cython.

Dag made a good point about functions having automatically converted
python objects as arguments. I would suggest an easy solution is to
just forbid to use python objects as reference argument. To get back
to Dag's example, as a user, I would not consider having to write the
following a big burden:

f(y)  #rejected by cython with a helpful error message, so I know
there is a subtlety here

# after understanding the issue, I can just write:
int z=y
f(z)  # OK
y=z  #

Also, note that the majority (I think) of C++ functions having
reference argument do so just to avoid the cost of a copy of the
argument (when this cost is high) and use "const references". These
functions require no special handling, and  trying to synchronize the
value of the python object with its automatically created C equivalent
would add useless overhead (there is no hard guaranty that a function
declaring a "const reference" argument will not modify it though, but
that would be very bad behaviour).

cheers,
 Toki
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to