std::sort using a compare object
From the example here, add this to myclass:
myclass() {printf("CONSTRUCTED\n");}
~myclass() {printf("DESTRUCTED\n");}
There are 8 elements. Sort does 20 compares. The class myclass is constructed once, followed by 10 stupid calls to the destructor during sorting, and then the correct call to the destructor happens as the program exits.
The reason: The copy constructor is being invoked a bunch of times. Now, in my opinion, sorting should generally be fast. Copying an entire object it not fast. There's ovbiously a conflict of interest here. Perhaps the reason is std::sort passing the object around recursively, but why not just pass by reference???
Being able to pass an object to std::sort is convenient especially for thread safety. The alternative, and my solution, is to declare a compare friend-function and set a static pointer (not thread-safe) to the object with the relevent data.