Created
August 27, 2016 15:40
-
-
Save nikhedonia/d4ab355eb2ab7fc4cc0a48da261f0127 to your computer and use it in GitHub Desktop.
Gist created by https://fiddle.jyt.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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