Best way to pass a function as a parameter

In C ++, there are several ways to pass a function as a parameter, but I would like to understand what are the advantages and advantages of each of them, for example, looking at the signature of functions from the algorithm:

template <class RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

Why does the std algorithm use a template for the pass function instead of the std :: function?

Why the use of threads does not use the functions of semantics and algorithms?

template <class Fn, class... Args>
explicit thread (Fn&& fn, Args&&... args);

PS: I ignore the methods used in C as function pointers.

+4
source share
2 answers

Why does the std algorithm use a template for the pass function instead of the std :: function?

++ 98 std::function. , Comparator , -, , ; Compare - , operator() . std::function .

?

++ 98 rvalue.

, , rvalue , ( ).

std::sort ? . .

+5

std- pass std:: function?

std::function , , comp ( , ..) , . std::function, ( ).

std::thread, , Fn Args, , .

?

, std::thread , , , , . , , .

UPDATE:

, , sort .

std::function. , std::thread - Fn Args ptr.

+1

All Articles