Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect type deduction of function pointer types #3180

Open
mje-nz opened this issue Oct 12, 2019 · 0 comments

Comments

@mje-nz
Copy link
Contributor

@mje-nz mje-nz commented Oct 12, 2019

I'm not sure exactly what the conditions are to reproduce this; I've been going through <algorithm> (see #3179) and a few (but not all) of the functions which take predicates misbehave.

Example (deduced as bool (char, char) instead of bool (*)(char, char)):

from libcpp cimport bool   
from libcpp.string cimport string  
 
cdef extern from "<algorithm>" namespace "std" nogil: 
    Iter1 search[Iter1, Iter2, BinaryPred](Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, BinaryPred pred) except +
     
cdef bool is_equal(char lhs, char rhs): 
    return lhs == rhs 
 
def in_quote(string quote, string word): 
    """ 
    Test search using cppreference example. 
 
    >>> in_quote(b"why waste time learning, when ignorance is instantaneous?", b"learning") 
    True 
    >>> in_quote(b"why waste time learning, when ignorance is instantaneous?", b"lemming") 
    False 
    """ 
    return search(quote.begin(), quote.end(), word.begin(), word.end(), is_equal) != word.end()
In file included from /Users/matthew/.ipython/cython/_cython_magic_f17ad9e8d5577187caa54cf4a2e070e8a5cf6430.cpp:674:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/ios:216:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/__locale:15:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/string:477:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/string_view:176:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/__string:56:
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:1637:12: error: no matching function for call to '__search'
    return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/__config:447:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
              ^
/Users/matthew/.ipython/cython/_cython_magic_f17ad9e8d5577187caa54cf4a2e070e8a5cf6430.cpp:1373:22: note: in instantiation of function template specialization
      'std::__1::search<std::__1::__wrap_iter<char *>, std::__1::__wrap_iter<char *>, bool (char, char)>' requested here
    __pyx_t_1 = std::search<std::string::iterator,std::string::iterator,bool (char, char)>(__pyx_v_quote.begin(), __pyx_v_quote.end(), __pyx_v_word.begin(), __pyx_v_word.end...
                     ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:1589:1: note: candidate function not viable: no known conversion from 'bool (*)(char, char)' to
      'bool (&)(char, char)' for 5th argument; dereference the argument with *
__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:1551:1: note: candidate function not viable: no known conversion from 'bool (*)(char, char)' to
      'bool (&)(char, char)' for 5th argument; dereference the argument with *
__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
^
1 error generated.

The generated code calls std::search<std::string::iterator, std::string::iterator, bool (char, char)>; adding an explicit cast to bool (*)(char, char) or using &is_equal works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.