Re: Long time no see and Input Capture
Posted: November 21st, 2018, 12:43 am
Ok, I think I'm on the right track. The point of the function class to is store any callable as you describe but with a common interface. std::function works for all standard function pointers; you have to change the declaration in order to get it to accept member functions. So, I wrap the function pointer call in a lambda and pass that to the std::function constructor. The idea here is to overload based on callable type to avoid overhead. Not sure how to test if the callable is a lambda though. Suppose I could make an overload for all non-function pointers. I'd still like to figure out why that code block above won't compile though.
Code: Select all
template <typename FUNC>
struct is_function_ptr
:
std::integral_constant<bool, std::is_pointer<FUNC>::value &&
std::is_function<typename std::remove_pointer<FUNC>::type>::value>
{};
template<typename RT, typename... Args>
class Function
{
public:
Function(void* p = nullptr) {}
template<typename FUNC, typename = std::enable_if_t<is_function_ptr<FUNC>::value>>
Function(FUNC&& func)
: action([func](Args&&... args)->RT {return (*func)(std::forward<Args>(args)...); })
{}
template<typename FUNC, typename O, typename = std::enable_if_t<std::is_member_function_pointer<FUNC>::value>>
Function(FUNC&& func, O* o)
: action([func, o](Args&&... args)->RT {return (o->*func)(std::forward<Args>(args)...); })
{}
template<typename FUNC, typename O, typename = std::enable_if_t<std::is_member_function_pointer<FUNC>::value>>
Function(FUNC&& func, O& o)
: action([func, &o](Args&&... args)->RT {return (o.*func)(std::forward<Args>(args)...); })
{}