Lionel Data | 3 Aug 10:53 2010
Picon

C++ wrapping : Template bug ?

Hi, I am experiencing a strange behavior of Cython when trying to wrap STL containers.

I copy/pasted the code available here : http://wiki.cython.org/WrappingCPlusPlus#Templates

This works fine if I keep "int" as the template parameter. It still works fine if I put "int *".

If I now add:

cdef extern from "test.h":
    cdef cppclass TestClass:
        TestClass()

below the list declaration, it still works fine with "TestClass" as the template param.

But surprisingly, it does not even compile to c++ if I write "TestClass *".
The error message is:

cdef vector[TestClass *] *v = new vector[TestClass *]()
                      ^
------------------------------------------------------------

/home/lionel/test/test_cython.pyx:21:23: Expected an identifier or literal

It seems like a bug, but I'm quite tired today so I might have missed something. If it is indeed a bug, what do you suggest as a workaround ?

(I use the repository "trunk" version)

Lionel

Lionel Data | 3 Aug 11:41 2010
Picon

Re: C++ wrapping : Template bug ?



2010/8/3 Lionel Data <lionel.data <at> gmail.com>
Hi, I am experiencing a strange behavior of Cython when trying to wrap STL containers.

I copy/pasted the code available here : http://wiki.cython.org/WrappingCPlusPlus#Templates

This works fine if I keep "int" as the template parameter. It still works fine if I put "int *".

If I now add:

cdef extern from "test.h":
    cdef cppclass TestClass:
        TestClass()

below the list declaration, it still works fine with "TestClass" as the template param.

But surprisingly, it does not even compile to c++ if I write "TestClass *".
The error message is:

cdef vector[TestClass *] *v = new vector[TestClass *]()
                      ^
------------------------------------------------------------

/home/lionel/test/test_cython.pyx:21:23: Expected an identifier or literal

It seems like a bug, but I'm quite tired today so I might have missed something. If it is indeed a bug, what do you suggest as a workaround ?

(I use the repository "trunk" version)

Lionel


Well, it wasn't that hard to fix after all, a "ctypedef struct PTestClass" did the job, along with a "typedef TestClass * PTestClass" in the .h file.


Lionel
Robert Bradshaw | 4 Aug 09:08 2010

Re: Re: C++ wrapping : Template bug ?

On Tue, Aug 3, 2010 at 2:41 AM, Lionel Data <lionel.data <at> gmail.com> wrote:
>
>
> 2010/8/3 Lionel Data <lionel.data <at> gmail.com>
>>
>> Hi, I am experiencing a strange behavior of Cython when trying to wrap STL
>> containers.
>>
>> I copy/pasted the code available here :
>> http://wiki.cython.org/WrappingCPlusPlus#Templates
>>
>> This works fine if I keep "int" as the template parameter. It still works
>> fine if I put "int *".
>>
>> If I now add:
>>
>> cdef extern from "test.h":
>>     cdef cppclass TestClass:
>>         TestClass()
>>
>> below the list declaration, it still works fine with "TestClass" as the
>> template param.
>>
>> But surprisingly, it does not even compile to c++ if I write "TestClass
>> *".
>> The error message is:
>>
>> cdef vector[TestClass *] *v = new vector[TestClass *]()
>>                       ^
>> ------------------------------------------------------------
>>
>> /home/lionel/test/test_cython.pyx:21:23: Expected an identifier or literal
>>
>> It seems like a bug, but I'm quite tired today so I might have missed
>> something. If it is indeed a bug, what do you suggest as a workaround ?
>>
>> (I use the repository "trunk" version)
>>
>> Lionel
>
>
> Well, it wasn't that hard to fix after all, a "ctypedef struct PTestClass"
> did the job, along with a "typedef TestClass * PTestClass" in the .h file.

Glad you found a workaround. C++ support (especially templates and
references) is still in the experimental phase (it should always
produce correct code when it compiles, it just might not compile all
you'd want it to). It looks like you've uncovered a bug, thanks.

- Robert


Gmane