Question: I use a (C) library with functions taking different parameters but always returning an int greater than zero in case of error:

```
int functionA(int param1, char param2); /* return an error code on failure, 0 otherwise */
int functionB(LIB_BOOLEAN param1);      /* return an error code on failure, 0 otherwise */
// ...

```

I would like to turn them all to be exception ready:

```
if (functionA(param1, param2) > 0)
{ throw std::runtime_error("Method failed"); }

```

Is it possible to write a template to do it once for all method ?

EDIT:
The idea is to avoid checking the result for each functions every time I use them.

Answer: 
Here is a template-based solution using C++17's new `auto` template parameters:

```
template <auto &> struct CallerImpl;

template <typename ...Args, int (&F)(Args ...)>
struct CallerImpl<F>
{
    static int CallOrThrow(Args ... args)
    {
        if (int n = F(args...); n > 0)
            throw std::runtime_error("Method failed");
        else
            return n;
    }
};

template <auto & F, typename ... Brgs>
int CallOrThrow(Brgs ... brgs)
{
    return CallerImpl<F>::CallOrThrow(brgs...);
}

```

Usage example:

```
int foo(double, char)
{
    std::cout << "Foo called\n";
    return 8;
}

CallOrThrow<foo>(1.5, 'x');

```
```
template <typename ... A> void executeFunction(int(*func_ptr)(A...), A...) {
   if (func_ptr(A...) > 0)
     throw std::runtime_error("Method failed"); 
}

int func(int A, int B) {
}

int main() {
    int a, b;
    a = 0;
    b = 4;
    executeFunction(func, a, b)
}

```

Not tested but something like this should work.

Hope it helps!