Pybind - 奇怪的重复模板
Pybind - Curiously Recurring Template
我有以下Class结构(我实际实现的简化示例):
/* 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");
}
我大致基于这个问题的答案 。但是我得到的错误是:
C2783 'pybind11::class_> &pybind11::class_>::def(const char *,Func &&,const Extra &...)': could not deduce template argument for 'Func' ...\source.cpp 10
C2672 'pybind11::class_>::def': no matching overloaded function found ...\source.cpp 12
这似乎与第二个 template <class FuncType>
有关,我无法在任何地方定义它,因为通用函数 func
将在稍后传入。有什么办法可以规避这个问题吗?
eval
必须是接受 double 和 returns double(或可转换为 double 的类型)的函数,因此您可以使用 &DeclareClass::eval<double(*)(double)>
专门化模板;或更好地包括 <functional>
和 <pybind11/functional.h>
,您可以完全删除模板并使 eval
将 std::function<double(double)>
作为其第一个参数。
更具体地说,我会改写如下
/* TestClass.hpp */
#pragma once
#include <functional>
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() { };
double eval(std::function<double(double)> func, double x) const
{
return this->impl().evalImplementation(func, x);
}
};
class DerivedClass : public BaseClass<DerivedClass> {
public:
double evalImplementation(std::function<double(double)> f, double x) const
{
return f(x);
};
};
/* Source.cpp */
#include <pybind11/pybind11.h>
#include "TestClass.hpp"
#include <pybind11/functional.h>
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(Project1, m) {
declare<DerivedClass>(m, "DerivedClass");
}
我有以下Class结构(我实际实现的简化示例):
/* 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");
}
我大致基于这个问题的答案
C2783 'pybind11::class_> &pybind11::class_>::def(const char *,Func &&,const Extra &...)': could not deduce template argument for 'Func' ...\source.cpp 10
C2672 'pybind11::class_>::def': no matching overloaded function found ...\source.cpp 12
这似乎与第二个 template <class FuncType>
有关,我无法在任何地方定义它,因为通用函数 func
将在稍后传入。有什么办法可以规避这个问题吗?
eval
必须是接受 double 和 returns double(或可转换为 double 的类型)的函数,因此您可以使用 &DeclareClass::eval<double(*)(double)>
专门化模板;或更好地包括 <functional>
和 <pybind11/functional.h>
,您可以完全删除模板并使 eval
将 std::function<double(double)>
作为其第一个参数。
更具体地说,我会改写如下
/* TestClass.hpp */
#pragma once
#include <functional>
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() { };
double eval(std::function<double(double)> func, double x) const
{
return this->impl().evalImplementation(func, x);
}
};
class DerivedClass : public BaseClass<DerivedClass> {
public:
double evalImplementation(std::function<double(double)> f, double x) const
{
return f(x);
};
};
/* Source.cpp */
#include <pybind11/pybind11.h>
#include "TestClass.hpp"
#include <pybind11/functional.h>
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(Project1, m) {
declare<DerivedClass>(m, "DerivedClass");
}