jeudi 24 août 2017

C++ ambiguity in function executor with arguments passed in vector

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> &params)
{
    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> &params)
{
    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:

  1. function cos works for both double and float, so call is ambiguous. Moreover, I cannot use double in place of typename to force it. Or there is other way?
  2. 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