pybind11:卡在未解析的重载函数类型中
pybind11: stuck with unresolved overloaded function type
我正在为 libnest2d 开发 pybind11 包装器。不幸的是,我并不精通现代 C++ 模板,并且陷入了这个过载问题。我尝试使用 py::overload_cast 解决它(请参阅底部的 src/main.cpp),但问题仍然存在。任何关于如何 analyse/solve 这将非常受欢迎的提示。
编译器必须说的话:
/usr/bin/c++ -DBOOST_ALL_NO_LIB -DLIBNEST2D_GEOMETRIES_clipper -DLIBNEST2D_OPTIMIZER_nlopt -DLIBNEST2D_THREADING_std -Dnest2d_EXPORTS -I/home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include -I/usr/include/python3.8 -I/home/mark/devel/14_2D_3D/nest2d/lib/libnest2d/include -isystem /usr/include/polyclipping -DVERSION_INFO=\"0.1.0\" -O3 -DNDEBUG -fPIC -fvisibility=hidden -std=c++14 -flto -fno-fat-lto-objects -o CMakeFiles/nest2d.dir/src/main.cpp.o -c /home/mark/devel/14_2D_3D/nest2d/src/main.cpp
/home/mark/devel/14_2D_3D/nest2d/src/main.cpp: In function ‘void pybind11_init_nest2d(pybind11::module&)’:
/home/mark/devel/14_2D_3D/nest2d/src/main.cpp:62:85: error: no match for call to ‘(const pybind11::detail::overload_cast_impl<std::vector<libnest2d::_Item<ClipperLib::Polygon>, std::allocator<libnest2d::_Item<ClipperLib::Polygon> > >&, const libnest2d::_Box<ClipperLib::IntPoint>&>) (<unresolved overloaded function type>)’
62 | m.def("nest", py::overload_cast<std::vector<Item>&, const Box&>(&libnest2d::nest)
| ^
In file included from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/pytypes.h:12,
from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/cast.h:13,
from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/attr.h:13,
from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/pybind11.h:44,
from /home/mark/devel/14_2D_3D/nest2d/src/main.cpp:1:
/home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/detail/common.h:736:20: note: candidate: ‘template<class Return> constexpr decltype (pf) pybind11::detail::overload_cast_impl<Args>::operator()(Return (*)(Args ...)) const [with Return = Return; Args = {std::vector<libnest2d::_Item<ClipperLib::Polygon>, std::allocator<libnest2d::_Item<ClipperLib::Polygon> > >&, const libnest2d::_Box<ClipperLib::IntPoint>&}]’
736 | constexpr auto operator()(Return (*pf)(Args...)) const noexcept
| ^~~~~~~~
/home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/detail/common.h:736:20: note: template argument deduction/substitution failed:
/home/mark/devel/14_2D_3D/nest2d/src/main.cpp:62:85: note: couldn’t deduce template parameter ‘Return’
62 | m.def("nest", py::overload_cast<std::vector<Item>&, const Box&>(&libnest2d::nest)
| ^
In file included from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/pytypes.h:12,
from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/cast.h:13,
from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/attr.h:13,
from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/pybind11.h:44,
from /home/mark/devel/14_2D_3D/nest2d/src/main.cpp:1:
/home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/detail/common.h:740:20: note: candidate: ‘template<class Return, class Class> constexpr decltype (pmf) pybind11::detail::overload_cast_impl<Args>::operator()(Return (Class::*)(Args ...), std::false_type) const [with Re
src/main.cpp:
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <libnest2d/libnest2d.hpp>
namespace py = pybind11;
using Point = libnest2d::Point;
using Box = libnest2d::Box;
using Item = libnest2d::Item;
PYBIND11_MODULE(nest2d, m)
{
m.doc() = "2D irregular bin packaging and nesting for python";
py::class_<Point>(m, "Point", "2D Point")
.def(py::init<int, int>(), py::arg("x"), py::arg("y"))
//.def_property_readonly("x", &Point::X) // TODO
//.def_property_readonly("y", &Point::Y)
.def("__repr__",
[](const Point &p) {
std::string r("Point(");
r += boost::lexical_cast<std::string>(p.X);
r += ", ";
r += boost::lexical_cast<std::string>(p.Y);
r += ")";
return r;
}
)
.def("__eq__",
[](const Point &p, const Point & q) {
return p == q;
}
);
// see lib/libnest2d/include/libnest2d/geometry_traits.hpp
py::class_<Box>(m, "Box", "2D Box point pair")
.def(py::init<int, int>())
;
// Item is a shape defined by points
// see lib/libnest2d/include/libnest2d/nester.hpp
py::class_<Item>(m, "Item", "An item to be placed on a bin.")
.def(py::init<std::vector<Point>>())
.def("__repr__",
[](const Item &i) {
std::string r("Item area: ");
r += boost::lexical_cast<std::string>(i.area());
return r;
}
)
;
// The nest function takes two parameters input and box
// see lib/libnest2d/include/libnest2d/libnest2d.hpp
m.def("nest", py::overload_cast<std::vector<Item>&, const Box&>(&libnest2d::nest)
)
;
}
要使其成为 MRE,我们还需要 CMakeLists.txt(cmake 构建):
cmake_minimum_required(VERSION 2.8.12)
set(CMAKE_VERBOSE_MAKEFILE ON)
project(nest2d)
# Boost and its components
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
add_subdirectory(lib/pybind11)
add_subdirectory(lib/libnest2d)
pybind11_add_module(nest2d src/main.cpp)
target_link_libraries(nest2d PUBLIC libnest2d)
问题与转换中使用的参数数量有关。一开始我太专注于简单案例的实现而没有看到这一点。感谢 Wim Lavrijsen 指出这一点(见评论)。
我发现在这种情况下,我 根本不需要 overload_cast,但可以明确地中继呼叫。
m.def("nest", [](std::vector<Item> input, const Box& box) {
return libnest2d::nest(input, box);
}
);
我正在为 libnest2d 开发 pybind11 包装器。不幸的是,我并不精通现代 C++ 模板,并且陷入了这个过载问题。我尝试使用 py::overload_cast 解决它(请参阅底部的 src/main.cpp),但问题仍然存在。任何关于如何 analyse/solve 这将非常受欢迎的提示。
编译器必须说的话:
/usr/bin/c++ -DBOOST_ALL_NO_LIB -DLIBNEST2D_GEOMETRIES_clipper -DLIBNEST2D_OPTIMIZER_nlopt -DLIBNEST2D_THREADING_std -Dnest2d_EXPORTS -I/home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include -I/usr/include/python3.8 -I/home/mark/devel/14_2D_3D/nest2d/lib/libnest2d/include -isystem /usr/include/polyclipping -DVERSION_INFO=\"0.1.0\" -O3 -DNDEBUG -fPIC -fvisibility=hidden -std=c++14 -flto -fno-fat-lto-objects -o CMakeFiles/nest2d.dir/src/main.cpp.o -c /home/mark/devel/14_2D_3D/nest2d/src/main.cpp
/home/mark/devel/14_2D_3D/nest2d/src/main.cpp: In function ‘void pybind11_init_nest2d(pybind11::module&)’:
/home/mark/devel/14_2D_3D/nest2d/src/main.cpp:62:85: error: no match for call to ‘(const pybind11::detail::overload_cast_impl<std::vector<libnest2d::_Item<ClipperLib::Polygon>, std::allocator<libnest2d::_Item<ClipperLib::Polygon> > >&, const libnest2d::_Box<ClipperLib::IntPoint>&>) (<unresolved overloaded function type>)’
62 | m.def("nest", py::overload_cast<std::vector<Item>&, const Box&>(&libnest2d::nest)
| ^
In file included from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/pytypes.h:12,
from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/cast.h:13,
from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/attr.h:13,
from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/pybind11.h:44,
from /home/mark/devel/14_2D_3D/nest2d/src/main.cpp:1:
/home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/detail/common.h:736:20: note: candidate: ‘template<class Return> constexpr decltype (pf) pybind11::detail::overload_cast_impl<Args>::operator()(Return (*)(Args ...)) const [with Return = Return; Args = {std::vector<libnest2d::_Item<ClipperLib::Polygon>, std::allocator<libnest2d::_Item<ClipperLib::Polygon> > >&, const libnest2d::_Box<ClipperLib::IntPoint>&}]’
736 | constexpr auto operator()(Return (*pf)(Args...)) const noexcept
| ^~~~~~~~
/home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/detail/common.h:736:20: note: template argument deduction/substitution failed:
/home/mark/devel/14_2D_3D/nest2d/src/main.cpp:62:85: note: couldn’t deduce template parameter ‘Return’
62 | m.def("nest", py::overload_cast<std::vector<Item>&, const Box&>(&libnest2d::nest)
| ^
In file included from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/pytypes.h:12,
from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/cast.h:13,
from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/attr.h:13,
from /home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/pybind11.h:44,
from /home/mark/devel/14_2D_3D/nest2d/src/main.cpp:1:
/home/mark/devel/14_2D_3D/nest2d/lib/pybind11/include/pybind11/detail/common.h:740:20: note: candidate: ‘template<class Return, class Class> constexpr decltype (pmf) pybind11::detail::overload_cast_impl<Args>::operator()(Return (Class::*)(Args ...), std::false_type) const [with Re
src/main.cpp:
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <libnest2d/libnest2d.hpp>
namespace py = pybind11;
using Point = libnest2d::Point;
using Box = libnest2d::Box;
using Item = libnest2d::Item;
PYBIND11_MODULE(nest2d, m)
{
m.doc() = "2D irregular bin packaging and nesting for python";
py::class_<Point>(m, "Point", "2D Point")
.def(py::init<int, int>(), py::arg("x"), py::arg("y"))
//.def_property_readonly("x", &Point::X) // TODO
//.def_property_readonly("y", &Point::Y)
.def("__repr__",
[](const Point &p) {
std::string r("Point(");
r += boost::lexical_cast<std::string>(p.X);
r += ", ";
r += boost::lexical_cast<std::string>(p.Y);
r += ")";
return r;
}
)
.def("__eq__",
[](const Point &p, const Point & q) {
return p == q;
}
);
// see lib/libnest2d/include/libnest2d/geometry_traits.hpp
py::class_<Box>(m, "Box", "2D Box point pair")
.def(py::init<int, int>())
;
// Item is a shape defined by points
// see lib/libnest2d/include/libnest2d/nester.hpp
py::class_<Item>(m, "Item", "An item to be placed on a bin.")
.def(py::init<std::vector<Point>>())
.def("__repr__",
[](const Item &i) {
std::string r("Item area: ");
r += boost::lexical_cast<std::string>(i.area());
return r;
}
)
;
// The nest function takes two parameters input and box
// see lib/libnest2d/include/libnest2d/libnest2d.hpp
m.def("nest", py::overload_cast<std::vector<Item>&, const Box&>(&libnest2d::nest)
)
;
}
要使其成为 MRE,我们还需要 CMakeLists.txt(cmake 构建):
cmake_minimum_required(VERSION 2.8.12)
set(CMAKE_VERBOSE_MAKEFILE ON)
project(nest2d)
# Boost and its components
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
add_subdirectory(lib/pybind11)
add_subdirectory(lib/libnest2d)
pybind11_add_module(nest2d src/main.cpp)
target_link_libraries(nest2d PUBLIC libnest2d)
问题与转换中使用的参数数量有关。一开始我太专注于简单案例的实现而没有看到这一点。感谢 Wim Lavrijsen 指出这一点(见评论)。 我发现在这种情况下,我 根本不需要 overload_cast,但可以明确地中继呼叫。
m.def("nest", [](std::vector<Item> input, const Box& box) {
return libnest2d::nest(input, box);
}
);