As a follow-up to Can tr1::function swallow return values?, how can one work around the limitation that tr1::function
cannot swallow return values?
This should work in the specific case of swallowing the return value of a callable object taking no arguments:
template<typename FuncT>
struct swallow_return_t
{
explicit swallow_return_t(FuncT i_Func):m_Func(i_Func){}
void operator()(){ m_Func(); }
FuncT m_Func;
};
template<typename FuncT>
swallow_return_t<FuncT>
swallow_return(FuncT f)
{
return swallow_return_t<FuncT>(f);
}
Then use like:
int Foo();
std::tr1::function<void()> Bar = swallow_return(Foo);
I assume variadic templates and perfect forwarding would permit generalization of this technique to arbitrary parameter lists. Is there a better way?