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

no one special

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!

Jarod42

You might use std::index_sequence, something like:

template <typename... Types, std::size_t ... Is>
double execute(double (*func)(Types...),
               const std::vector<double> &params,
               std::index_sequence<Is...>)
{
    if (params.size() != sizeof...(Types)) {
        throw std::runtime_error("Wrong number of function arguments");
    }
    return func(params[Is]...);
}

template <typename... Types>
double execute(double (*func)(Types...), const std::vector<double> &params)
{
    return execute(func, params, std::index_sequence_for<Types...>());
}

And call it (specify template argument to fix overload).

double execute(const std::string &functionName, const std::vector<double> &params)
{
    if (functionName == "cos") return (execute<double>)(cos, params);
    if (functionName == "atan2") return (execute<double, double>)(atan2, params);
    throw std::runtime_error("Unknown function name");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C - passing function with arguments into wrapper executor

From Dev

Arguments not passed to function in c Kernel

From Dev

Printing integers passed as arguments in function in C

From Dev

Checking function calls are not passed as arguments (C macros)

From Dev

How are arguments passed to function pointers in C?

From Dev

Printing integers passed as arguments in function in C

From Dev

Arguments passed to function using stack in C

From Dev

The arguments are not passed into the function correctly

From Dev

Objects modified when they are passed to a function as arguments in C++

From Dev

Catching variables passed to function with no arguments

From Dev

Bash function arguments not passed as expected

From Dev

Access arguments of a function passed as argument

From Dev

Catching variables passed to function with no arguments

From Dev

Vector of async functions that receives arguments passed by reference

From Dev

Vector iterator passed to function error

From Dev

C++: Vector of function pointers to functions with different type of arguments

From Dev

Counting arguments passed to scanf() in C

From Dev

C++ overloaded function pointer ambiguity

From Dev

C++ overloaded function pointer ambiguity

From Dev

Ambiguity in Function overloading in C++ Vs Java

From Dev

Matlab function with two vector arguments

From Dev

Lua: Quoted arguments passed as one in function

From Dev

R pattern to modify arguments passed to generic function

From Dev

Creating a list of lists passed as arguments to the function in LISP

From Dev

Pass additional arguments to the function passed in map()

From Dev

Decorators: how arguments are passed to wrapped function?

From Dev

phpunit testing, check the arguments passed to function

From Java

Variadic function with arguments of different type passed to it

From Dev

WinDBG View Passed Arguments to Any Function

Related Related

  1. 1

    C - passing function with arguments into wrapper executor

  2. 2

    Arguments not passed to function in c Kernel

  3. 3

    Printing integers passed as arguments in function in C

  4. 4

    Checking function calls are not passed as arguments (C macros)

  5. 5

    How are arguments passed to function pointers in C?

  6. 6

    Printing integers passed as arguments in function in C

  7. 7

    Arguments passed to function using stack in C

  8. 8

    The arguments are not passed into the function correctly

  9. 9

    Objects modified when they are passed to a function as arguments in C++

  10. 10

    Catching variables passed to function with no arguments

  11. 11

    Bash function arguments not passed as expected

  12. 12

    Access arguments of a function passed as argument

  13. 13

    Catching variables passed to function with no arguments

  14. 14

    Vector of async functions that receives arguments passed by reference

  15. 15

    Vector iterator passed to function error

  16. 16

    C++: Vector of function pointers to functions with different type of arguments

  17. 17

    Counting arguments passed to scanf() in C

  18. 18

    C++ overloaded function pointer ambiguity

  19. 19

    C++ overloaded function pointer ambiguity

  20. 20

    Ambiguity in Function overloading in C++ Vs Java

  21. 21

    Matlab function with two vector arguments

  22. 22

    Lua: Quoted arguments passed as one in function

  23. 23

    R pattern to modify arguments passed to generic function

  24. 24

    Creating a list of lists passed as arguments to the function in LISP

  25. 25

    Pass additional arguments to the function passed in map()

  26. 26

    Decorators: how arguments are passed to wrapped function?

  27. 27

    phpunit testing, check the arguments passed to function

  28. 28

    Variadic function with arguments of different type passed to it

  29. 29

    WinDBG View Passed Arguments to Any Function

HotTag

Archive