You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a class template Parameter that i would like to bind with python accepting different types
This is the code structure
// in parameters.h
template<typename T>
class BaseParameter {
/* ....... */
}
template<typename T>
class Parameter;
// Specifying Derived Parameter types
template<>
class Parameter<double> : public BaseParameter<double> {
/* ....... */
}
template<>
class Parameter<int> : public BaseParameter<int> {
/* ....... */
}
And binding with pybind
// In parameters.cpp
void wrap_double_parameter(py::module& mod) {
// Trampoline definition
class PyDoubleParameter : public Parameter<double>
{
public:
using Parameter::Parameter;
/* ....... */
}
//
py::class_<Parameter<double>, PyDoubleParameter> PAR(mod, "Parameter");
PAR
.def(/* ....... */)
}
void wrap_int_parameter(py::module& mod) {
class PyIntParameter : public Parameter<int>
{
public:
using Parameter::Parameter;
/* ....... */
}
//
py::class_<Parameter<int>, PyIntParameter> PAR(mod, "Parameter");
PAR
.def(/* ....... */)
}
PYBIND11_MODULE(parameters, mod) {
wrap_double_parameter(mod);
wrap_int_parameter(mod);
}
I suspect the problem is that both classes share the name "Parameter". However, that is my intention, in that, in Python I would like a single class Parameter that would only accept the specified types. This leads to the error:
'ImportError: generic_type: cannot initialize type "Parameter": an object with that name is already defined'
Is it possible to bind a single class template with multiple types? Rather is there a workaround this?
(NOTE: for the sake of brevity i specified the types int and double however there is also an Eigen::VectorXd that would be an accepted type for Parameter. Also, I wouldnt want BaseParameter to be accesible in Python, hence why i only binded Parameter, unless i have to?)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a class template
Parameter
that i would like to bind with python accepting different typesThis is the code structure
And binding with pybind
I suspect the problem is that both classes share the name
"Parameter"
. However, that is my intention, in that, in Python I would like a single classParameter
that would only accept the specified types. This leads to the error:Is it possible to bind a single class template with multiple types? Rather is there a workaround this?
(NOTE: for the sake of brevity i specified the types
int
anddouble
however there is also anEigen::VectorXd
that would be an accepted type forParameter
. Also, I wouldnt wantBaseParameter
to be accesible in Python, hence why i only bindedParameter
, unless i have to?)Beta Was this translation helpful? Give feedback.
All reactions