C++ pybind11:无法解析重载函数类型

C++ pybind11:无法解析重载函数类型,c++,pybind11,C++,Pybind11,我正在为您制作pybind11包装。毫无疑问,我不太精通现代C++模板,并被这个超载问题所困扰。我试图使用py::overload_cast解决这个问题(请参阅底部的src/main.cpp),但问题仍然存在。任何关于如何分析/解决这一问题的提示都是非常受欢迎的 编译器必须说明的内容: /usr/bin/c++ -DBOOST_ALL_NO_LIB -DLIBNEST2D_GEOMETRIES_clipper -DLIBNEST2D_OPTIMIZER_nlopt -DLIBNEST2

我正在为您制作pybind11包装。毫无疑问,我不太精通现代C++模板,并被这个超载问题所困扰。我试图使用py::overload_cast解决这个问题(请参阅底部的src/main.cpp),但问题仍然存在。任何关于如何分析/解决这一问题的提示都是非常受欢迎的

编译器必须说明的内容:

    /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

该问题与铸造中使用的参数数量有关。我一开始太专注于简单案例的实现,而没有看到这一点。感谢维姆·拉夫里森指出这一点(见评论)。 我发现在这种情况下,我根本不需要重载,但可以显式地中继调用

m.def("nest", [](std::vector<Item> input, const Box& box) {
        return libnest2d::nest(input, box);
    }
    );
m.def(“嵌套”,[](标准::矢量输入,常量框和方框){
返回libnest2d::nest(输入,框);
}
);

仅仅意味着重载(即带有参数
std::vector&,const Box&
的重载)无法匹配。如果我找到正确的标题,那么它看起来像“代码> Nest 需要至少5个参数。从Read MD的“简单”C++示例只使用两个参数(这是工作):SisieHT BIs=Nest(输入,框(150000000, 150000000));因此,参数计数似乎不是问题所在。这是非常不同的:调用函数时,会考虑默认值,不需要显式提供。然而,这里是创建强制转换的模板解析,而不是函数调用。这是一个很好的洞察,有助于解决此问题。非常感谢你!
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)
m.def("nest", [](std::vector<Item> input, const Box& box) {
        return libnest2d::nest(input, box);
    }
    );