Edit Oh, I mentioned the obvious in chat:
@EthanSteinberg: lambdas?
[] (int realparam, int dummy) { return foo(realparam); }
But it was dismissed, which is why I jump to:
Edit I just realized a much simpler approach: http://ideone.com/pPWZk
#include <iostream>
#include <functional>
using namespace std::placeholders;
int foo(int i)
{
return i*2;
}
int main(int argc, const char *argv[])
{
std::function<int(int, int)> barfunc = std::bind(foo, (_1, _2));
std::cout << barfunc(-999, 21) << std::endl;
// or even (thanks Xeo)
barfunc = std::bind(foo, _2);
std::cout << barfunc(-999, 21) << std::endl;
}
A somewhat longer answer based on variadic templates would result in possibly smaller code at the call site (if you wanted to wrap functions with a long argument list).
#include <iostream>
#include <functional>
int foo(int i)
{
return i*2;
}
template <typename Ax, typename R, typename... A>
struct Wrap
{
typedef R (*F)(A...);
typedef std::function<R(A...)> Ftor;
Wrap(F f) : _f(f) { }
Wrap(const Ftor& f) : _f(f) { }
R operator()(Ax extra, A... a) const
{ return _f(a...); /*just forward*/ }
Ftor _f;
};
template <typename Ax=int, typename R, typename... A>
std::function<R(Ax, A...)> wrap(R (f)(A...))
{
return Wrap<Ax,R,A...>(f);
}
template <typename Ax=int, typename R, typename... A>
std::function<R(Ax, A...)> wrap(std::function<R(A...)> functor)
{
return Wrap<Ax,R,A...>(functor);
}
int main(int argc, const char *argv[])
{
auto bar = wrap(foo);
std::function<int(int, int)> barfunc = wrap(foo);
std::cout << barfunc(-999, 21) << std::endl;
// wrap the barfunc?
auto rewrap = wrap(barfunc);
std::cout << rewrap(-999, -999, 21) << std::endl;
return 0;
}
Generalizing from this would require some more heavy lifting. I think I've seen in the past helpers to 'dissect' (using meta-programming) the signature of a std::function<> and you should be able to make it recognize non-void functions, and perhaps even adding a parameter at the end or in the middle (tricky, as far as I can tell now).
But for your simple case from the OP, it looks like you're covered