I have a C ++ class called Graph, and it has a for_each_node () algorithm method. I can either make it a template, for example:
template <class UnaryFunction> UnaryFunction Graph::for_each_node (UnaryFunction f) { }
or use its std :: function, for example:
typedef std::function<void (Node&)> ForEachNodeFunc; ForEachNodeFunc Graph::for_each_node (ForEachNodeFunc f) { }
Standard algorithms, for example. std :: for_each, use the first approach, while some libraries, for example. gtkmm (which is a binding to C ++ GTK +) accepts functions as pointers to functions of objects containing them.
What are the advantages and disadvantages of each option? I'm not sure which one to choose. What should influence the choice: is my Graph class a class template, or how many different functions are supposed to be used with an algorithm method or speed requirements?
cfa45ca55111016ee9269f0a52e771
source share