Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nikhedonia/d4ab355eb2ab7fc4cc0a48da261f0127 to your computer and use it in GitHub Desktop.
Save nikhedonia/d4ab355eb2ab7fc4cc0a48da261f0127 to your computer and use it in GitHub Desktop.
Gist created by https://fiddle.jyt.io
#include<functional>
template<class T>
struct AsFunction
: public AsFunction<decltype(&T::operator())>
{};
template<class ReturnType, class... Args>
struct AsFunction<ReturnType(Args...)> {
using type = std::function<ReturnType(Args...)>;
};
template<class ReturnType, class... Args>
struct AsFunction<ReturnType(*)(Args...)> {
using type = std::function<ReturnType(Args...)>;
};
template<class Class, class ReturnType, class... Args>
struct AsFunction<ReturnType(Class::*)(Args...) const> {
using type = std::function<ReturnType(Args...)>;
};
template<class F>
auto toFunction( F f ) -> typename AsFunction<F>::type {
return {f};
}
template <typename T>
void stdfunc_test(std::function<T(T)> func) {};
int test_func(int arg)
{
return arg + 2;
}
int main()
{
stdfunc_test( toFunction([](int _) {return _ + 2;}) );
stdfunc_test( toFunction(test_func) );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment