Why are there two implementations of std::sort (with and without a comparator) rather than one implementation with a default template parameter?
        Posted  
        
            by 
                PolyVox
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by PolyVox
        
        
        
        Published on 2012-06-29T15:12:18Z
        Indexed on 
            2012/06/29
            15:15 UTC
        
        
        Read the original article
        Hit count: 259
        
In my code I'm adopting a design strategy which is similar to some standard library algorithms in that the exact behavior can be customized by a function object. The simplest example is std::sort, where a function object can control how the comparison is made between objects.
I notice that the Visual C++ provides two implementations of std::sort, which naturally involves code duplication. I would have imagined that it was instead possible to have only one implementation, and provide a default comparator (using operator< ) as a default template parameter.
What is the rational behind two separate versions? Would my suggestion make the interface more complex in some way? Or result in confusing error messages when the object does not provide operator
Thanks,
David
© Stack Overflow or respective owner