У меня есть следующая структура класса (упрощенный пример моей фактической реализации):
/* TestClass.hpp */
#pragma once
template <class Impl>
class CurRecTemplate {
protected:
CurRecTemplate() {}
~CurRecTemplate() {}
Impl& impl() { return static_cast<Impl&>(*this); }
const Impl& impl() const { return static_cast<const Impl&>(*this); }
};
template <class Impl>
class BaseClass : public CurRecTemplate<Impl> {
public:
BaseClass() { };
template <class FuncType>
double eval(const FuncType& func, double x) const
{
return this->impl().evalImplementation(func, x);
}
};
class DerivedClass : public BaseClass<DerivedClass> {
public:
template <class FuncType>
double evalImplementation(const FuncType& f, double x) const
{
return f(x);
};
};
, а затем
/* Source.cpp */
#include <pybind11/pybind11.h>
#include "TestClass.hpp"
namespace py = pybind11;
template<typename Impl>
void declare(py::module &m, const std::string& className) {
using DeclareClass = BaseClass<Impl>;
py::class_<DeclareClass, std::shared_ptr<DeclareClass>>(m, className.c_str())
.def(py::init<>())
.def("eval", &DeclareClass::eval);
}
PYBIND11_MODULE(PyBindTester, m) {
declare<DerivedClass>(m, "DerivedClass");
}
который я свободно основал на ответе на этот вопрос Шаблон класса PyBind11 многих типов . Однако ошибки, которые я получаю:
C2783 'pybind11 :: class_> & pybind11 :: class _> :: def (const char *, Func &&, const Extra & ...)': не удалось вывести аргумент шаблона для 'Func' ... \ source. cpp 10
C2672 'pybind11 :: class _> :: def': не найдена соответствующая перегруженная функция ... \ source.cpp 12
Кажется, это связано со вторым template <class FuncType>
, который я нигде не могу определить, так как обобщенная функция func
будет передана позже. Есть ли способ обойти эту проблему?