0

I have the following Class structure (simplified example of my actual implementation):

/* 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);
    };
};

and then

/* 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");
}

which I loosely based on the answer to this question PyBind11 Template Class of Many Types. However the errors I get are:

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

It seems to have to do with the second template <class FuncType> which I cannot define anywhere since the generic function func will be passed in later. Is there any way to circumvent this issue?

Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40

1 Answers1

1

eval has to be a function that accepts a double and returns a double (or types convertible to double), so you can specialize the template with &DeclareClass::eval<double(*)(double)>; or better include <functional> and <pybind11/functional.h> and you can remove the template altogether and make eval take a std::function<double(double)> as its first parameter.

To be more specific, I would rewrite as follows

/* 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");
}
Dominic Price
  • 1,111
  • 8
  • 19