I am fighting with this variadic template for a long time. Could anyone help me please? I would like to build an executor that is able to call cmath functions and pass all its parameters through vector. Please consider a following code:
bool execute(const std::string &functionName, const std::vector<double> ¶ms)
{
if (functionName == "cos") return execute(cos, params);
if (functionName == "atan2") return execute(atan2, params);
return false;
}
Function cos
takes one parameter while atan2
takes two. I wanted to have something like this:
template <typename... Types>
bool execute(double (*func)(Types...), const std::vector<double> ¶ms)
{
if (params.size() != sizeof...(Types)) {
errorString = "Wrong number of function arguments";
return false;
}
errno = 0;
result = func(params[0]);
errorString = strerror(errno);
return !errno;
}
However, I encountered two problems:
- function
cos
works for bothdouble
andfloat
, so call is ambiguous. Moreover, I cannot usedouble
in place oftypename
to force it. Or there is other way? - When I am trying to call function
func
how can I specify right amount of arguments from vector depending on type of function?
Or maybe there is something already available in C++ which I do not know about? :) Many Thanks!
Aucun commentaire:
Enregistrer un commentaire