no known conversion error in gcc

84 views
Skip to first unread message

buc...@gmail.com

unread,
Oct 4, 2016, 3:05:07 AM10/4/16
to cython-users
I have a strange problem working with C++ booleans using C++11 constructs. I have a small repro:

from libcpp cimport bool as cbool
from libcpp.vector cimport vector
from libcpp.memory cimport shared_ptr, make_shared
from cython.operator cimport dereference as deref

def test_vector():
    cdef shared_ptr[vector[cbool]] vec
    vec = make_shared[vector[cbool]]()
    for i in range(3):
        deref(vec).push_back(True)

    cdef cbool item = deref(vec).at(1)



Cython (0.24) compiles this without any complaints using this command:



cython --cplus -3 --fast-fail test_vec.pyx




However, gcc (4.9) fails using this command:


gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python3.3m -std=c++11 -o test_vec.so test_vec.cpp




The error message from gcc is:



test_vec.cpp: In function ‘PyObject* __pyx_pf_8test_vec_2test_vector(PyObject*)’:
test_vec.cpp:950:15: error: no match for ‘operator=’ (operand types are ‘__Pyx_FakeReference<bool>’ and ‘std::vector<bool>::reference {aka std::_Bit_reference}’)
     __pyx_t_3 = (*__pyx_v_vec).at(1);
               ^
test_vec.cpp:950:15: note: candidates are:
test_vec.cpp:224:7: note: __Pyx_FakeReference<bool>& __Pyx_FakeReference<bool>::operator=(const __Pyx_FakeReference<bool>&)
 class __Pyx_FakeReference {
       ^
test_vec.cpp:224:7: note:   no known conversion for argument 1 from ‘std::vector<bool>::reference {aka std::_Bit_reference}’ to ‘const __Pyx_FakeReference<bool>&’
test_vec.cpp:224:7: note: __Pyx_FakeReference<bool>& __Pyx_FakeReference<bool>::operator=(__Pyx_FakeReference<bool>&&)
test_vec.cpp:224:7: note:   no known conversion for argument 1 from ‘std::vector<bool>::reference {aka std::_Bit_reference}’ to ‘__Pyx_FakeReference<bool>&&’


However, if I change all the `cbool` references to `int` in the pyx file, it compiles and runs happily. Am I misusing the bool operator?

Thanks,


Dusty




Robert Bradshaw

unread,
Oct 4, 2016, 8:05:13 PM10/4/16
to cython...@googlegroups.com
Ah, std::vector<bool>, surprising people since 1998.

The problem is that std::vector<bool> is not really a stl container,
and members like operator[] and at don't actually return bools or bool
references. http://www.cplusplus.com/reference/vector/vector-bool/

There aren't really any good solutions here. Your best bet is to us an
int (or char, which is smaller) as the vector type instead.

I filed https://github.com/cython/cython/issues/1477
> --
>
> ---
> 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.
> For more options, visit https://groups.google.com/d/optout.

buc...@gmail.com

unread,
Oct 6, 2016, 8:55:19 AM10/6/16
to cython-users
Wow, that is.... surprising. Thanks for the clarification and explanation. Unfortunately I don't have control over the type of the vector's template, as it has to be passed into a C++ function who's signature I don't have control over. I guess I'll just write a little adapter in C++ that does the conversion for me.

Thanks,

Dusty

Robert Bradshaw

unread,
Oct 6, 2016, 2:50:54 PM10/6/16
to cython...@googlegroups.com
BTW, operator[] should work fine, as we don't have to allocate a temporary.

deref(vec).at[1] # doesn't check error bounds

Robert Bradshaw

unread,
Oct 6, 2016, 3:42:28 PM10/6/16
to cython...@googlegroups.com
And I just pushed a special case fix which will be out in the next release.
Reply all
Reply to author
Forward
0 new messages