There is no theoretical reason why sort should be faster than qsort . Some compilers will even embed the function pointer passed to qsort as functions: I think I saw gcc or clang (this is not for qsort ), and even when the function definition was in another cpp file.
The important part is that sort gets the function object of both type and instance. Another function is created for each such type: template are factories for functions. At the point where it is called, the exact function called is really easy to determine for each instance of such a function, so embedding is trivial.
Performing the same action using the function pointer is possible, but requires insertion from the point at which qsort is called, and careful monitoring of the immutability of the function pointer and determining the function with which it should begin. This is much more fragile than the mechanism described above in practice.
Similar problems appear with the element pitch (obviously static, when sort with an array, itβs more difficult to deal with qsort ), etc.
source share