pybind / pybind/pybind11

Unable to use bind_vector for types with unusual comparison operators

Open
#1,470 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Issue description

When a type has operator== defined, but it returns something other than bool or something that can be converted to bool, the detection logic in container_traits<T>::test_comparable fails and returns true when it should return false. Then code gets instantiated in vector_if_equal_operator which then fails to compile.

It should additionally to the existence of operator== be checked whether the return value can be converted to bool. But my metaprogramming skills are not good enough to come up with a clean solution...

Reproducible example code

#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>

template<typename T>
struct vec { T a; T b; };

template<typename T>
vec<bool> operator==(const vec<T>& l, const vec<T>& r) {
    return {l.a == r.a, l.b == r.b};
}

PYBIND11_MODULE(test, m) {
    pybind11::bind_vector<std::vector<vec<float>>>(m, "float_vec_vector");
}

The actual type for which I ran into this is cv::Mat from OpenCV. And I worked around it for now with:

namespace pybind11::detail {
    template<> struct is_comparable<cv::Mat> : std::false_type {};
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at container_traits::test_comparable and follow its use in vector_if_equal_operator when bind_vector is instantiated. Reproduce the failure with the shown vec, non-bool operator==, and pybind11::bind_vector example; done means the unusual comparison is rejected before incompatible code is instantiated and the example compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.