Why do we use std::function in C++ rather than the original C function pointer? [duplicate]

std::function can hold more than function pointers, namely functors. #include <functional> void foo(double){} struct foo_functor{ void operator()(float) const{} }; int main(){ std::function<void(int)> f1(foo), f2((foo_functor())); f1(5); f2(6); } Live example on Ideone. As the example shows, you also don’t need the exact same signature, as long as they are compatible (i.e., the parameter type of std::function … Read more

What is the purpose of std::function and how to use it?

std::function is a type erasure object. That means it erases the details of how some operations happen, and provides a uniform run time interface to them. For std::function, the primary1 operations are copy/move, destruction, and ‘invocation’ with operator() — the ‘function like call operator’. In less abstruse English, it means that std::function can contain almost … Read more

can void* be used to store function pointers? [duplicate]

Maybe. Until C++11, they couldn’t; but C++11 adds: Converting a function pointer to an object pointer type or vice versa is conditionally-supported. The meaning of such a conversion is implementation-defined, except that if an implementation supports conversions in both directions, converting a prvalue of one type to the other type and back, possibly with different … Read more

c++ Implementing Timed Callback function

For a portable solution, you can use boost::asio. Below is a demo I wrote a while ago. You can change t.expires_from_now(boost::posix_time::seconds(1)); to suit you need say make function call after 200 milliseonds. t.expires_from_now(boost::posix_time::milliseconds(200)); Below is a complete working example. It’s calling repeatedly but I think it should be easy to call only once by just … Read more