struct foo_overload_set_t {
template<typename... Ts>
auto operator()(Ts&&... ts) const
-> decltype( foo(std::forward<Ts>(ts)...) ) {
return ( foo(std::forward<Ts>(ts)...) );
}
};
static const foo_overload_set_t foo_overload_set;
now foo_overload_set
is a single object which can be treated like a function, and when invoked does overload resolution on the function foo
based off the passed in parameters.
Ie, foo( a, b, c, d, e )
runs the same code and gives the exact same result as foo_overload_set( a, b, c, d, e )
for any set of arguments.
But foo_overload_set
is an object, while foo
is an indeterminate set of functions created on the fly based off parameters used to call it.
We can pass said object to your for_each
:
template<typename OverloadSet>
void for_each( OverloadSet ) {} // do nothing
template < typename OverloadSet , typename T , typename... TT >
void for_each ( OverloadSet overload, T&& parameter , TT&&... other_parameters )
{
overload( std::forward<T>(parameter) );
for_each( overload, std::forward<TT>(other_parameters)... );
}
invoked like:
for_each( foo_overload_set, a, b, c, d, e );
which proceeds to call foo
on a
, then on b
, then on c
, etc.