#include <unistd.h>
#include <thread>
#include <chrono>
#include <mutex>
#include <functional>
#include <iostream>
#include <cmath>
template <const size_t _UniqueId, typename _Res, typename... _ArgTypes>
struct fun_ptr_helper
{
public:
typedef std::function<_Res(_ArgTypes...)> function_type;
static void bind(function_type&& f)
{ instance().fn_.swap(f); }
static void bind(const function_type& f)
{ instance().fn_=f; }
static _Res invoke(_ArgTypes... args)
{ return instance().fn_(args...); }
typedef decltype(&fun_ptr_helper::invoke) pointer_type;
static pointer_type ptr()
{ return &invoke; }
private:
static fun_ptr_helper& instance()
{
static fun_ptr_helper inst_;
return inst_;
}
fun_ptr_helper() {}
function_type fn_;
};
template <const size_t _UniqueId, typename _Res, typename... _ArgTypes>
typename fun_ptr_helper<_UniqueId, _Res, _ArgTypes...>::pointer_type
get_fn_ptr(const std::function<_Res(_ArgTypes...)>& f)
{
fun_ptr_helper<_UniqueId, _Res, _ArgTypes...>::bind(f);
return fun_ptr_helper<_UniqueId, _Res, _ArgTypes...>::ptr();
}
template<typename T>
std::function<typename std::enable_if<std::is_function<T>::value, T>::type>
make_function(T *t)
{
return {t};
}
int main()
{
std::cout << (void*)get_fn_ptr<0>(make_function(::sin))<<std::endl;
return 0;
}
I2luY2x1ZGUgPHVuaXN0ZC5oPgojaW5jbHVkZSA8dGhyZWFkPgojaW5jbHVkZSA8Y2hyb25vPgojaW5jbHVkZSA8bXV0ZXg+CiNpbmNsdWRlIDxmdW5jdGlvbmFsPgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxjbWF0aD4KCnRlbXBsYXRlIDxjb25zdCBzaXplX3QgX1VuaXF1ZUlkLCB0eXBlbmFtZSBfUmVzLCB0eXBlbmFtZS4uLiBfQXJnVHlwZXM+CnN0cnVjdCBmdW5fcHRyX2hlbHBlcgp7CnB1YmxpYzoKICAgIHR5cGVkZWYgc3RkOjpmdW5jdGlvbjxfUmVzKF9BcmdUeXBlcy4uLik+IGZ1bmN0aW9uX3R5cGU7CgogICAgc3RhdG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