std::function is pretty cool at hiding that stuff and is first-class, so it opens some of the flexibility of that world up to C++. I think for a member function the overhead is an extra call due to how it generically binds things (union hacks ftw >.<).
Gaze at the madness that is a variadic template adder (got bored) ye mighty and despair:
template<typename... ARGS> struct add { };
template<typename A, typename B> struct add<A,B>
{
typedef const A& param1;
typedef const B& param2;
typedef decltype(A()+B()) rettype;
rettype operator()(param1 a, param2 b) const
{
return a + b;
}
};
template<typename A, typename B, typename C> struct add<A,B,C>
{
typedef const A& param1;
typedef const B& param2;
typedef const C& param3;
typedef add<A,B> abadder;
typedef typename abadder::rettype retabtype;
typedef add<retabtype,C> abcadder;
typedef typename abcadder::rettype retabctype;
typedef retabctype rettype;
rettype operator()(param1 a, param2 b, param3 c) const
{
return abcadder()(abadder()(a,b), c);
}
};
template<typename A, typename B, typename C, typename... D> struct add<A,B,C,D...>
{
typedef const A& param1;
typedef const B& param2;
typedef const C& param3;
typedef add<A,B> abadder;
typedef typename abadder::rettype retabtype;
typedef add<retabtype,C> abcadder;
typedef typename abcadder::rettype retabctype;
typedef retabctype rettype;
rettype operator()(param1 a, param2 b, param3 c, const D& ... d) const
{
return add<retabctype, D...>()(abcadder()(abadder()(a,b), c), d...);
}
};
template<typename... A> typename add<A...>::rettype addition(const A&... a)
{
return add<A...>()(a...);
}
Should preserve standard operator precedence for a + b + c + d + e, which is (((a + b) + c) + d) + e. The easier way of doing this, simply to recursively call a + sum(b...) would do a + (b + (c + (d + e))). Well, that kept me distracted for half an hour wading through template errors -_-
You could probably implement such a thing without C++11 features (replacing decltype would be tricky though) via loads more specialisations created by a script.
Now I just need to find a use for this. Aside from accidentally discovering that gcc has some features with variadics not yet implemented in 4.6.0.
EDIT: And now I've genericnessificated it so it'll work for all operations. Yep, really bored.