Skip to content

False positives from is_copy_constructible with C++17 #1885

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

Closed
sgsaenger opened this issue Aug 15, 2019 · 1 comment · Fixed by #1886
Closed

False positives from is_copy_constructible with C++17 #1885

sgsaenger opened this issue Aug 15, 2019 · 1 comment · Fixed by #1886

Comments

@sgsaenger
Copy link
Contributor

When PYBIND11_CPP17 is defined, detail::is_copy_constructible may give false positives for complex structures, leading to compiler errors.

py::cast(std::map<int, std::unique_ptr<int>>{}); // does compile
py::cast(std::map<int, std::map<int, std::unique_ptr<int>>>{}); // does not compile

Undefining the macro, respectively unguarding the third variant of is_copy_constructible solves this problem.
Tested with gcc and clang(libc++) and pybind v2.3.0.

@wangxf123456
Copy link
Contributor

This might still be a problem for other complex structures right now:

struct vector_unique_ptr_owner {
  std::vector<std::unique_ptr<int>> vec;
};

void try_static_assert() {
  static_assert(
    !pybind11::detail::is_copy_constructible<vector_unique_ptr_owner>::value,
    "Needs to be false");  // Fails

One workaround for this is to explicitly tell pybind11 that the object is not copy constructible:

struct vector_unique_ptr_owner {
  std::vector<std::unique_ptr<int>> vec;
};

namespace pybind11 {
namespace detail {

template <>
struct is_copy_constructible<vector_unique_ptr_owner>: std::false_type {};

}  // namespace detail
}  // namespace pybind11

void try_static_assert() {
  static_assert(
      !py::detail::is_copy_constructible<vector_unique_ptr_owner>::value,
      "Needs to be false");  // Compiles
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants